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

Leveling up your git skills

Leveling up your git skills

This talk is designed to help those who know a little bit about git and give them a bit of knowledge and some confidence to level up those skills.

Chris Keathley

May 06, 2017
Tweet

More Decks by Chris Keathley

Other Decks in Programming

Transcript

  1. Becoming a Pro
    at Git
    Chris Keathley / @ChrisKeathley / [email protected]

    View Slide

  2. I work with a
    distributed team

    View Slide

  3. I work with a
    distributed team

    View Slide

  4. View Slide

  5. View Slide

  6. “We don’t have the luxury of a focus room”

    View Slide

  7. Everything is a-
    synchronous

    View Slide

  8. submitPR()
    .then(waitForApproval)
    .then(mergePR)
    .then(startDeployFromSlack)

    View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. Image of green merge button

    View Slide

  13. History is
    critical

    View Slide

  14. View Slide

  15. View Slide

  16. $ git checkout c652b6b
    Note: checking out 'c652b6b'.
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    git checkout -b
    HEAD is now at c652b6b... Add kubernetes setup to fish shell.

    View Slide

  17. You are in 'detached HEAD' state.

    View Slide

  18. 'detached HEAD'

    View Slide

  19. (Picture of a detached head)

    View Slide

  20. $ hg merge --help --verbose | wc -w
    # => 327

    View Slide

  21. $ hg merge --help --verbose | wc -w
    # => 327
    $ git merge --help | wc -w
    # => 3958

    View Slide

  22. View Slide

  23. Lets talk about…
    The golden rule of git
    Tools
    Scenarios

    View Slide

  24. Git’s
    Golden Rule

    View Slide

  25. View Slide

  26. If you don’t panic you can
    fix 100% of problems

    View Slide

  27. Tooling

    View Slide

  28. Git

    View Slide

  29. Git
    No, Seriously

    View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. You Git CLI

    View Slide

  34. CliTools

    View Slide

  35. Aliases

    View Slide

  36. alias g='git'
    alias gaa='git add -A'
    alias gb='git branch'
    alias gc='git commit'
    alias gco='git checkout'
    alias gd='git diff'
    alias gglr='git pull --rebase'
    alias ggp='git push'
    alias gst='git status'

    View Slide

  37. alias wip='git add -A && git commit -m "WIP"'
    alias gc!='git commit -v --amend'
    alias ggp!='git push --force'

    View Slide

  38. alias ibase='git rebase -i $(git merge-base HEAD master)'
    alias gpr='git pull-request -o'
    alias gcm='git remote show origin \
    | awk '/HEAD branch:/ {print \$3}' \
    | xargs git checkout'

    View Slide

  39. Diffs

    View Slide

  40. diff-so-fancy

    View Slide

  41. Diff-So-Fancy

    View Slide

  42. diff-so-fancy

    View Slide

  43. View Slide

  44. $ git checkout https://github.com/github/hub/pull/134
    Checkout PR

    View Slide

  45. Apply patch
    $ git am https://github.com/xoebus/hub/commit/177eeb8

    View Slide

  46. $ git pull-request
    Submit PR from cli

    View Slide

  47. Git Scripts

    View Slide

  48. Git Scripts
    λ cat ~/.bin/git-guilt
    #!/bin/sh
    git commit --amend --author "$1 <$2>" -C HEAD

    View Slide

  49. Git Scripts
    λ git guilt "Grace Hopper" "[email protected]"
    You need a passphrase to unlock the secret key for
    user: "Chris Keathley "
    4096-bit RSA key, ID EA3A16BD, created 2017-04-23
    [test-branch 0beb550] New file for demo
    Author: Grace Hopper
    Date: Wed May 3 21:54:10 2017 -0400
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 new_file

    View Slide

  50. Scenarios

    View Slide

  51. View Slide

  52. Scenario:
    Your first Bug

    View Slide

  53. View Slide

  54. Don’t Panic

    View Slide

  55. Git Bisect

    View Slide

  56. λ git bisect start HEAD HEAD~10

    View Slide

  57. λ git bisect start HEAD HEAD~10
    Start a new bisect

    View Slide

  58. λ git bisect start HEAD HEAD~10
    Start a new bisect
    The “bad” revision

    View Slide

  59. λ git bisect start HEAD HEAD~10
    Start a new bisect
    The “bad” revision
    The “Good” revision

    View Slide

  60. λ git bisect start HEAD HEAD~10

    View Slide

  61. λ git bisect start HEAD HEAD~10

    View Slide

  62. λ git bisect good

    View Slide

  63. λ git bisect good

    View Slide

  64. λ git bisect good

    View Slide

  65. λ git bisect bad

    View Slide

  66. λ git bisect bad

    View Slide

  67. λ git bisect bad

    View Slide

  68. λ git bisect bad

    View Slide

  69. λ git bisect bad
    6fb10e5ec916063e4b4bd744678559afb45d6791 is the first bad commit
    commit 6fb10e5ec916063e4b4bd744678559afb45d6791
    Author: Aaron Renner
    Date: Tue May 2 07:55:01 2017 -0600
    Fix leaking webdriver sessions in QueryTest (#209)
    Wallaby.Integration.QueryTest was starting a ton of sessions and not
    cleaning them up. This switches it over to use
    Wallaby.Integration.SessionCase and uses the session that's
    automatically created and cleaned up. I also extracted the unit tests
    out of this integration test so they can be run separately.

    View Slide

  70. 6fb10e5ec916063e4b4bd744678559afb45d6791

    View Slide

  71. λ git show -p 6fb10e5ec916063e4b4bd744678559afb45d6791

    View Slide

  72. You just found the code
    that introduced the bug!

    View Slide

  73. (Picture of Wizard)

    View Slide

  74. $ cat test_script.sh
    #!/bin/bash
    if tests_pass; then exit 1; else exit 0; fi

    View Slide

  75. $ git bisect run test_script.sh

    View Slide

  76. View Slide

  77. A Brief interlude to discuss
    the value of Commit history

    View Slide

  78. Bisect only works
    if the repo history
    is clean

    View Slide

  79. wip merge
    Repo History
    wip

    View Slide

  80. If you history looks like
    this its almost impossible
    to use bisects effectively

    View Slide

  81. Make commits
    small atomic
    units of
    awesome!

    View Slide

  82. Scenario:
    Start a feature

    View Slide

  83. View Slide

  84. Lets talk about
    branching

    View Slide

  85. Some of y’all have
    some crazy-ass
    branching strats

    View Slide

  86. Development
    Production

    View Slide

  87. View Slide

  88. Master
    Branch
    How we merge our code

    View Slide

  89. $ git checkout master
    $ git pull --rebase
    $ git checkout -b feature/my-fancy-form-123456

    View Slide

  90. feature/my-fancy-form-123456

    View Slide

  91. feature/
    chores/
    bug/

    View Slide

  92. View Slide

  93. When
    suddenly…

    View Slide

  94. View Slide

  95. Don’t Panic

    View Slide

  96. $ git add -A && git commit -m "WIP"
    $ git push -u origin HEAD
    Wip && Push

    View Slide

  97. Now your work is
    saved and shared

    View Slide

  98. $ git checkout master
    $ git checkout -b bugs/put-out-server-fire

    View Slide

  99. View Slide

  100. View Slide

  101. Picture of blank stare

    View Slide

  102. What
    happened?

    View Slide

  103. $ git checkout master
    $ git checkout -b bugs/put-out-server-fire

    View Slide

  104. $ git checkout master
    $ git pull —rebase
    $ git checkout -b bugs/put-out-server-fire

    View Slide

  105. Master

    View Slide

  106. Master

    View Slide

  107. Master

    View Slide

  108. Master

    View Slide

  109. Master
    Conflict between files

    View Slide

  110. Don’t Panic

    View Slide

  111. $ git rebase master

    View Slide

  112. $ git rebase master

    View Slide

  113. $ git rebase master

    View Slide

  114. Scenario:
    Fixing history

    View Slide

  115. $ git checkout master
    $ git pull --rebase
    $ git checkout -
    $ git rebase master

    View Slide

  116. $ git checkout master
    $ git pull --rebase
    $ git checkout -
    $ git rebase master

    View Slide

  117. $ git checkout master
    $ git pull --rebase
    $ git checkout -
    $ git rebase master

    View Slide

  118. $ git checkout master
    $ git pull --rebase
    $ git checkout -
    $ git rebase master

    View Slide

  119. λ git rebase master
    First, rewinding head to replay your work on top of it...
    Applying: My Fancy Fix
    Using index info to reconstruct a base tree...
    M README.md
    Falling back to patching base and 3-way merge...
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    error: Failed to merge in the changes.
    Patch failed at 0001 WIP
    The copy of the patch that failed is found in: .git/rebase-apply/patch
    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

    View Slide

  120. Resolve Conflicts

    View Slide

  121. Resolve Conflicts
    $ git add -A
    $ git rebase —continue

    View Slide

  122. Resolve Conflicts
    $ git push —force

    View Slide

  123. View Slide

  124. Scenario:
    Finishing our
    feature

    View Slide

  125. View Slide

  126. Picture of coffee

    View Slide

  127. $ git checkout feature/my-fancy-feature
    Checkout the feature branch

    View Slide

  128. View Slide

  129. WIP
    commit
    New
    Stuff

    View Slide

  130. WIP
    commit
    New
    Stuff

    View Slide

  131. WIP
    commit
    New
    Stuff
    other
    stuff

    View Slide

  132. Interactive
    Stage

    View Slide

  133. View Slide

  134. View Slide

  135. View Slide

  136. View Slide

  137. View Slide

  138. View Slide

  139. View Slide

  140. View Slide

  141. View Slide

  142. View Slide

  143. View Slide

  144. View Slide

  145. $ git commit
    Commit the current changes

    View Slide

  146. $ git add -A
    $ git commit
    Commit everything else

    View Slide

  147. $ git add -A
    $ git commit
    Commit everything else

    View Slide

  148. WIP
    commit
    New
    Stuff
    other
    stuff

    View Slide

  149. other
    stuff
    New
    Stuff

    View Slide

  150. Interactive
    Rebase

    View Slide

  151. $ git rebase -i HEAD~3
    Interactive Rebase

    View Slide

  152. View Slide

  153. View Slide

  154. View Slide

  155. View Slide

  156. View Slide

  157. other
    stuff
    New
    Stuff

    View Slide

  158. Scenario:
    Loosing a commit

    View Slide

  159. View Slide

  160. $ git rebase -i HEAD~10

    View Slide

  161. λ git rebase master
    First, rewinding head to replay your work on top of it...
    Applying: My Fancy Fix
    Using index info to reconstruct a base tree...
    M README.md
    Falling back to patching base and 3-way merge...
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    error: Failed to merge in the changes.
    Patch failed at 0001 WIP
    The copy of the patch that failed is found in: .git/rebase-apply/patch
    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

    View Slide

  162. $ git rebase --continue

    View Slide

  163. λ mix test
    1) test it works (Metaform.Web.PageViewTest)
    test/web/views/page_view_test.exs:4
    This is broken now
    stacktrace:
    test/web/views/page_view_test.exs:5: (test)
    ....
    Finished in 0.1 seconds
    5 tests, 1 failure

    View Slide

  164. View Slide

  165. Picture of you crying

    View Slide

  166. Your work was
    lost in a conflict

    View Slide

  167. Don’t Panic

    View Slide

  168. $ git reflog

    View Slide

  169. The reflog is all of
    your repos history

    View Slide

  170. $ git reflog

    View Slide

  171. f1b1 HEAD@{0}: rebase -i (finish): returning to refs/heads/chores/remove-select
    677f1b1 HEAD@{1}: rebase -i (pick): Update new file
    f55b0d8 HEAD@{2}: cherry-pick: fast-forward
    f4b7d8b HEAD@{3}: rebase -i (start): checkout HEAD~3
    ca47093 HEAD@{4}: commit: Update new file
    039d19a HEAD@{5}: commit (amend): My super important commit don't loose this.
    62fc606 HEAD@{6}: rebase -i (finish): returning to refs/heads/chores/remove-select

    View Slide

  172. f1b1 HEAD@{0}: rebase -i (finish): returning to refs/heads/chores/remove-select
    677f1b1 HEAD@{1}: rebase -i (pick): Update new file
    f55b0d8 HEAD@{2}: cherry-pick: fast-forward
    f4b7d8b HEAD@{3}: rebase -i (start): checkout HEAD~3
    ca47093 HEAD@{4}: commit: Update new file
    039d19a HEAD@{5}: commit (amend): My super important commit don't loose this.
    62fc606 HEAD@{6}: rebase -i (finish): returning to refs/heads/chores/remove-select

    View Slide

  173. 039d19a

    View Slide

  174. $ git show -p 039d19a
    $ git reset —hard 039d19a

    View Slide

  175. View Slide

  176. Conclusion

    View Slide

  177. Resources
    Diff-so-fancy - https://github.com/so-fancy/diff-so-fancy
    Hub - https://github.com/github/hub
    Slides - https://speakerdeck.com/keathley

    View Slide

  178. CLI

    View Slide

  179. Tools

    View Slide

  180. Branching

    View Slide

  181. Rebase

    View Slide

  182. Reflog

    View Slide

  183. Don’t Panic

    View Slide

  184. Thanks!
    Chris Keathley
    @ChrisKeathley
    [email protected]

    View Slide