Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git: Advanced Usage

Git: Advanced Usage

Trevor Strieber

November 17, 2015
Tweet

More Decks by Trevor Strieber

Other Decks in Programming

Transcript

  1. I'm not a nice person, and I don't care about

    you. I care about the technology and the kernel—that's what's important to me. --Linus Torvalds INSPIRATION FROM THE CREATOR
  2. $ git add --patch lib/person.rb diff --git a/lib/person.rb b/lib/person.rb index

    b02c09b..32be950 100644 --- a/lib/person.rb +++ b/lib/person.rb @@ -1,4 +1,15 @@ +# This is just an example comment. + module GitExample class Person + attr_reader :name + + def initialize(name) + @name = name + end + + def to_s + "<Person name: #{@name}>" + end end end Stage this hunk [y,n,q,a,d,/,e,?]? s
  3. Split into 2 hunks. @@ -1,2 +1,4 @@ +# This

    is just an example comment. + module GitExample class Person Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y @@ -1,4 +3,13 @@ module GitExample class Person + attr_reader :name + + def initialize(name) + @name = name + end + + def to_s + "<Person name: #{@name}>" + end end end Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n
  4. $ git status On branch master Changes to be committed:

    (use "git reset HEAD <file>..." to unstage) modified: lib/person.rb Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: lib/person.rb
  5. $ git rebase --interactive HEAD~2 pick 246eb8c Add comment. pick

    88a0494 Flesh out Person class. # Rebase b1ce32e..88a0494 onto b1ce32e (2 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
  6. $ git rebase --interactive HEAD~2 pick 246eb8c Add comment. squash

    88a0494 Flesh out Person class. # Rebase b1ce32e..88a0494 onto b1ce32e (2 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
  7. # This is a combination of 2 commits. # The

    first commit's message is: Add comment. # This is the 2nd commit message: Flesh out Person class. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Mon Nov 16 19:11:43 2015 -0800 # # interactive rebase in progress; onto b1ce32e # Last commands done (2 commands done): # pick 246eb8c Add comment. # squash 88a0494 Flesh out Person class. # No commands remaining. # You are currently editing a commit while rebasing branch 'master' on # 'b1ce32e'. # # Changes to be committed: # modified: lib/person.rb #
  8. [detached HEAD 7ea9f24] Add comment. Date: Mon Nov 16 19:11:43

    2015 -0800 1 file changed, 11 insertions(+) Successfully rebased and updated refs/heads/master. $ git log commit 7ea9f24bc4aa8172ca21d79b1f9d2fe632556a95 Author: Trevor Strieber <[email protected]> Date: Mon Nov 16 19:11:43 2015 -0800 Add comment. Flesh out Person class.
  9. $ git checkout master $ git pull --rebase origin master

    remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 1), reused 4 (delta 1), pack-reused 0 Unpacking objects: 100% (4/4), done. From github.com:TrevorS/git-example * branch master -> FETCH_HEAD 633c64a..d386601 master -> origin/master First, rewinding head to replay your work on top of it... Fast-forwarded master to d386601db239150e2c5d5335581fa55a39b03d7a.
  10. $ git checkout -b add-dog $ vim lib/dog.rb lib/person.rb $

    git log --oneline b14ae6c Adding Dog to Person. d9b1899 Adding name to Dog. 183d95c Started creating the Dog class. $ git checkout master $ git merge --squash add-dog Updating d386601..b14ae6c Fast-forward Squash commit -- not updating HEAD lib/dog.rb | 13 +++++++++++++ lib/person.rb | 7 ++++--- 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 lib/dog.rb $ git commit
  11. $ git log commit cdec91e798d0ccef52b607a74d06e75462b8915d Author: Trevor Strieber <[email protected]> Date:

    Tue Nov 17 14:44:52 2015 -0800 Squashed commit of the following: commit b14ae6c8a8110c18b27f3c661000fa1e77646282 Author: Trevor Strieber <[email protected]> Date: Tue Nov 17 14:34:42 2015 -0800 Adding Dog to Person. commit d9b18999595294ac2958ac2527ed19e23c570d5c Author: Trevor Strieber <[email protected]> Date: Tue Nov 17 14:33:55 2015 -0800 Adding name to Dog. commit 183d95c2febbccd12254b985166ce70c256cde52 Author: Trevor Strieber <[email protected]> Date: Tue Nov 17 14:32:51 2015 -0800 Started creating the Dog class.
  12. $ git config --global rerere.enabled true $ cat ~/.gitconfig [user]

    email = [email protected] name = Trevor Strieber [push] default = matching [core] excludesfile = ~/.gitignore_global [init] templatedir = ~/.git_template [diff] tool = vimdiff [difftool] prompt = false [rerere] enabled = true
  13. $ git log --oneline --patch adjust-person 7ef9b80 name -> first_name.

    diff --git a/lib/person.rb b/lib/person.rb index 32be950..5498f42 100644 --- a/lib/person.rb +++ b/lib/person.rb @@ -2,14 +2,14 @@ module GitExample class Person - attr_reader :name + attr_reader :first_name - def initialize(name) - @name = name + def initialize(first_name) + @first_name = first_name end def to_s - "<Person name: #{name}>" + "<Person first_name: #{@first_name}>" end end end
  14. $ git log --oneline --patch master f6e822d Add age. diff

    --git a/lib/person.rb b/lib/person.rb index 32be950..a8a495b 100644 --- a/lib/person.rb +++ b/lib/person.rb @@ -2,14 +2,15 @@ module GitExample class Person - attr_reader :name + attr_reader :name, :age - def initialize(name) + def initialize(name, age) @name = name + @age = age end def to_s - "<Person name: #{name}>" + "<Person name: #{@name}, age: #{@age}>" end end end
  15. $ git merge master Auto-merging lib/person.rb CONFLICT (content): Merge conflict

    in lib/person.rb Recorded preimage for 'lib/person.rb' Automatic merge failed; fix conflicts and then commit the result. $ vim lib/person.rb $ cat lib/person.rb # This is just an example comment. module GitExample class Person attr_reader :first_name, :age def initialize(first_name, age) @first_name = first_name @age = age end def to_s "<Person first_name: #{@first_name}, age: #{@age}>" end end end
  16. $ git status On branch adjust-person You have unmerged paths.

    (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: lib/person.rb no changes added to commit (use "git add" and/or "git commit -a") $ git add lib/person.rb $ git commit Recorded resolution for 'lib/person.rb'. [adjust-person bae7538] Merge branch 'master' into adjust-person $ ls -l .git/rr-cache/5414dbb6213644146d189ae4262d1c05197ba664 total 16 -rw-r--r-- 1 trevor staff 284 Nov 17 10:30 postimage -rw-r--r-- 1 trevor staff 428 Nov 17 10:20 preimage
  17. $ git reset --hard HEAD^ HEAD is now at 7ef9b80

    name -> first_name. $ git merge master Auto-merging lib/person.rb CONFLICT (content): Merge conflict in lib/person.rb Resolved 'lib/person.rb' using previous resolution. Automatic merge failed; fix conflicts and then commit the result. $ cat lib/person.rb # This is just an example comment. module GitExample class Person attr_reader :first_name, :age def initialize(first_name, age) @first_name = first_name @age = age end def to_s "<Person first_name: #{@first_name}, age: #{@age}>" end end end
  18. Capitalized, short (50 chars or less) summary More detailed explanatory

    text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. Write your commit message in the imperative: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, followed by a single space, with blank lines in between, but conventions vary here - Use a hanging indent
  19. Intelligence is the ability to avoid doing work, yet getting

    the work done. --Linus Torvalds MORE INSPIRATION