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.

06f8b41980eb4c577fa40c41d5030c19?s=128

Chris Keathley

May 06, 2017
Tweet

Transcript

  1. Becoming a Pro at Git Chris Keathley / @ChrisKeathley /

    c@keathley.io
  2. I work with a distributed team

  3. I work with a distributed team

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

  7. Everything is a- synchronous

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

  9. None
  10. None
  11. None
  12. Image of green merge button

  13. History is critical

  14. None
  15. None
  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 <new-branch-name> HEAD is now at c652b6b... Add kubernetes setup to fish shell.
  17. You are in 'detached HEAD' state.

  18. 'detached HEAD'

  19. (Picture of a detached head)

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

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

    327 $ git merge --help | wc -w # => 3958
  22. None
  23. Lets talk about… The golden rule of git Tools Scenarios

  24. Git’s Golden Rule

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

  27. Tooling

  28. Git

  29. Git No, Seriously

  30. None
  31. None
  32. None
  33. You Git CLI

  34. CliTools

  35. Aliases

  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'
  37. alias wip='git add -A && git commit -m "WIP"' alias

    gc!='git commit -v --amend' alias ggp!='git push --force'
  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'
  39. Diffs

  40. diff-so-fancy

  41. Diff-So-Fancy

  42. diff-so-fancy

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

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

  46. $ git pull-request Submit PR from cli

  47. Git Scripts

  48. Git Scripts λ cat ~/.bin/git-guilt #!/bin/sh git commit --amend --author

    "$1 <$2>" -C HEAD
  49. Git Scripts λ git guilt "Grace Hopper" "grace@hopper.com" You need

    a passphrase to unlock the secret key for user: "Chris Keathley <c@keathley.io>" 4096-bit RSA key, ID EA3A16BD, created 2017-04-23 [test-branch 0beb550] New file for demo Author: Grace Hopper <grace@hopper.com> Date: Wed May 3 21:54:10 2017 -0400 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 new_file
  50. Scenarios

  51. None
  52. Scenario: Your first Bug

  53. None
  54. Don’t Panic

  55. Git Bisect

  56. λ git bisect start HEAD HEAD~10

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

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

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

    The “bad” revision The “Good” revision
  60. λ git bisect start HEAD HEAD~10

  61. λ git bisect start HEAD HEAD~10

  62. λ git bisect good

  63. λ git bisect good

  64. λ git bisect good

  65. λ git bisect bad

  66. λ git bisect bad

  67. λ git bisect bad

  68. λ git bisect bad

  69. λ git bisect bad 6fb10e5ec916063e4b4bd744678559afb45d6791 is the first bad commit

    commit 6fb10e5ec916063e4b4bd744678559afb45d6791 Author: Aaron Renner <aaron.renner@gmail.com> 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.
  70. 6fb10e5ec916063e4b4bd744678559afb45d6791

  71. λ git show -p 6fb10e5ec916063e4b4bd744678559afb45d6791

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

  73. (Picture of Wizard)

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

    exit 0; fi
  75. $ git bisect run test_script.sh

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

  78. Bisect only works if the repo history is clean

  79. wip merge Repo History wip

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

    use bisects effectively
  81. Make commits small atomic units of awesome!

  82. Scenario: Start a feature

  83. None
  84. Lets talk about branching

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

  86. Development Production

  87. None
  88. Master Branch How we merge our code

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

    checkout -b feature/my-fancy-form-123456
  90. feature/my-fancy-form-123456

  91. feature/ chores/ bug/

  92. None
  93. When suddenly…

  94. None
  95. Don’t Panic

  96. $ git add -A && git commit -m "WIP" $

    git push -u origin HEAD Wip && Push
  97. Now your work is saved and shared

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

  99. None
  100. None
  101. Picture of blank stare

  102. What happened?

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

  104. $ git checkout master $ git pull —rebase $ git

    checkout -b bugs/put-out-server-fire
  105. Master

  106. Master

  107. Master

  108. Master

  109. Master Conflict between files

  110. Don’t Panic

  111. $ git rebase master

  112. $ git rebase master

  113. $ git rebase master

  114. Scenario: Fixing history

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

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

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

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

    checkout - $ git rebase master
  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".
  120. Resolve Conflicts

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

  122. Resolve Conflicts $ git push —force

  123. None
  124. Scenario: Finishing our feature

  125. None
  126. Picture of coffee

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

  128. None
  129. WIP commit New Stuff

  130. WIP commit New Stuff

  131. WIP commit New Stuff other stuff

  132. Interactive Stage

  133. None
  134. None
  135. None
  136. None
  137. None
  138. None
  139. None
  140. None
  141. None
  142. None
  143. None
  144. None
  145. $ git commit Commit the current changes

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

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

  148. WIP commit New Stuff other stuff

  149. other stuff New Stuff

  150. Interactive Rebase

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

  152. None
  153. None
  154. None
  155. None
  156. None
  157. other stuff New Stuff

  158. Scenario: Loosing a commit

  159. None
  160. $ git rebase -i HEAD~10

  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".
  162. $ git rebase --continue

  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
  164. None
  165. Picture of you crying

  166. Your work was lost in a conflict

  167. Don’t Panic

  168. $ git reflog

  169. The reflog is all of your repos history

  170. $ git reflog

  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
  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
  173. 039d19a

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

  175. None
  176. Conclusion

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

  178. CLI

  179. Tools

  180. Branching

  181. Rebase

  182. Reflog

  183. Don’t Panic

  184. Thanks! Chris Keathley @ChrisKeathley c@keathley.io