Slide 1

Slide 1 text

$ git --advanced-usage [email protected]

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

keep your commits logical

Slide 4

Slide 4 text

git add --patch stage hunks of a file

Slide 5

Slide 5 text

$ 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 + "" + end end end Stage this hunk [y,n,q,a,d,/,e,?]? s

Slide 6

Slide 6 text

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 + "" + end end end Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n

Slide 7

Slide 7 text

$ git status On branch master Changes to be committed: (use "git reset HEAD ..." to unstage) modified: lib/person.rb Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: lib/person.rb

Slide 8

Slide 8 text

git rebase --interactive manipulate commit history

Slide 9

Slide 9 text

$ 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

Slide 10

Slide 10 text

$ 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

Slide 11

Slide 11 text

# 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 #

Slide 12

Slide 12 text

[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 Date: Mon Nov 16 19:11:43 2015 -0800 Add comment. Flesh out Person class.

Slide 13

Slide 13 text

keep your history clean

Slide 14

Slide 14 text

git pull --rebase git fetch && git rebase

Slide 15

Slide 15 text

$ 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.

Slide 16

Slide 16 text

merge vs. rebase

Slide 17

Slide 17 text

merge * creates merge commits * does not affect history * can create confusing history

Slide 18

Slide 18 text

rebase * rewinds before applying changes * maintains a linear commit flow * can be dangerous (!!)

Slide 19

Slide 19 text

don't rebase anything you have published rebase

Slide 20

Slide 20 text

git merge --squash aggregate before merging

Slide 21

Slide 21 text

$ 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

Slide 22

Slide 22 text

$ git log commit cdec91e798d0ccef52b607a74d06e75462b8915d Author: Trevor Strieber Date: Tue Nov 17 14:44:52 2015 -0800 Squashed commit of the following: commit b14ae6c8a8110c18b27f3c661000fa1e77646282 Author: Trevor Strieber Date: Tue Nov 17 14:34:42 2015 -0800 Adding Dog to Person. commit d9b18999595294ac2958ac2527ed19e23c570d5c Author: Trevor Strieber Date: Tue Nov 17 14:33:55 2015 -0800 Adding name to Dog. commit 183d95c2febbccd12254b985166ce70c256cde52 Author: Trevor Strieber Date: Tue Nov 17 14:32:51 2015 -0800 Started creating the Dog class.

Slide 23

Slide 23 text

git rerere save conflict resolutions

Slide 24

Slide 24 text

$ 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

Slide 25

Slide 25 text

$ 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 - "" + "" end end end

Slide 26

Slide 26 text

$ 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 - "" + "" end end end

Slide 27

Slide 27 text

$ 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 "" end end end

Slide 28

Slide 28 text

$ git status On branch adjust-person You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add ..." 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

Slide 29

Slide 29 text

$ 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 "" end end end

Slide 30

Slide 30 text

git commit messages

Slide 31

Slide 31 text

i stole this from @tpope

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

tips and tricks

Slide 34

Slide 34 text

git checkout - return to last branch

Slide 35

Slide 35 text

git log --patch show log with changes

Slide 36

Slide 36 text

git diff --ignore-all-space whitespace ignoring diff

Slide 37

Slide 37 text

git diff --check expose whitespace issues

Slide 38

Slide 38 text

$ git diff --check lib/person.rb:12: trailing whitespace. + ""██████

Slide 39

Slide 39 text

export EDITOR="atom --wait" set your editor

Slide 40

Slide 40 text

git config --global rerere.autoupdate true auto stage rerere'd files

Slide 41

Slide 41 text

git push origin --delete branch-name remove a remote branch

Slide 42

Slide 42 text

git (pull|merge|fetch|add) --help read the fine manual

Slide 43

Slide 43 text

Intelligence is the ability to avoid doing work, yet getting the work done. --Linus Torvalds MORE INSPIRATION

Slide 44

Slide 44 text

resources Online Git Manual: https://git-scm.com/doc Pro Git: https://git-scm.com/book/en/v2 GitHub: https://help.github.com/categories/using-git/ This Slideshow: https://speakerdeck.com/trevors/git-advanced-usage