Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

I work with a distributed team

Slide 3

Slide 3 text

I work with a distributed team

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Everything is a- synchronous

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Image of green merge button

Slide 13

Slide 13 text

History is critical

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

You are in 'detached HEAD' state.

Slide 18

Slide 18 text

'detached HEAD'

Slide 19

Slide 19 text

(Picture of a detached head)

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Lets talk about… The golden rule of git Tools Scenarios

Slide 24

Slide 24 text

Git’s Golden Rule

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Tooling

Slide 28

Slide 28 text

Git

Slide 29

Slide 29 text

Git No, Seriously

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

You Git CLI

Slide 34

Slide 34 text

CliTools

Slide 35

Slide 35 text

Aliases

Slide 36

Slide 36 text

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'

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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'

Slide 39

Slide 39 text

Diffs

Slide 40

Slide 40 text

diff-so-fancy

Slide 41

Slide 41 text

Diff-So-Fancy

Slide 42

Slide 42 text

diff-so-fancy

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

$ git pull-request Submit PR from cli

Slide 47

Slide 47 text

Git Scripts

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Scenarios

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Scenario: Your first Bug

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Don’t Panic

Slide 55

Slide 55 text

Git Bisect

Slide 56

Slide 56 text

λ git bisect start HEAD HEAD~10

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

λ git bisect start HEAD HEAD~10

Slide 61

Slide 61 text

λ git bisect start HEAD HEAD~10

Slide 62

Slide 62 text

λ git bisect good

Slide 63

Slide 63 text

λ git bisect good

Slide 64

Slide 64 text

λ git bisect good

Slide 65

Slide 65 text

λ git bisect bad

Slide 66

Slide 66 text

λ git bisect bad

Slide 67

Slide 67 text

λ git bisect bad

Slide 68

Slide 68 text

λ git bisect bad

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

6fb10e5ec916063e4b4bd744678559afb45d6791

Slide 71

Slide 71 text

λ git show -p 6fb10e5ec916063e4b4bd744678559afb45d6791

Slide 72

Slide 72 text

You just found the code that introduced the bug!

Slide 73

Slide 73 text

(Picture of Wizard)

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

$ git bisect run test_script.sh

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

A Brief interlude to discuss the value of Commit history

Slide 78

Slide 78 text

Bisect only works if the repo history is clean

Slide 79

Slide 79 text

wip merge Repo History wip

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Make commits small atomic units of awesome!

Slide 82

Slide 82 text

Scenario: Start a feature

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Lets talk about branching

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Development Production

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Master Branch How we merge our code

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

feature/my-fancy-form-123456

Slide 91

Slide 91 text

feature/ chores/ bug/

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

When suddenly…

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

Don’t Panic

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Now your work is saved and shared

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

Picture of blank stare

Slide 102

Slide 102 text

What happened?

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

Master

Slide 106

Slide 106 text

Master

Slide 107

Slide 107 text

Master

Slide 108

Slide 108 text

Master

Slide 109

Slide 109 text

Master Conflict between files

Slide 110

Slide 110 text

Don’t Panic

Slide 111

Slide 111 text

$ git rebase master

Slide 112

Slide 112 text

$ git rebase master

Slide 113

Slide 113 text

$ git rebase master

Slide 114

Slide 114 text

Scenario: Fixing history

Slide 115

Slide 115 text

$ git checkout master $ git pull --rebase $ git checkout - $ git rebase master

Slide 116

Slide 116 text

$ git checkout master $ git pull --rebase $ git checkout - $ git rebase master

Slide 117

Slide 117 text

$ git checkout master $ git pull --rebase $ git checkout - $ git rebase master

Slide 118

Slide 118 text

$ git checkout master $ git pull --rebase $ git checkout - $ git rebase master

Slide 119

Slide 119 text

λ 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".

Slide 120

Slide 120 text

Resolve Conflicts

Slide 121

Slide 121 text

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

Slide 122

Slide 122 text

Resolve Conflicts $ git push —force

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Scenario: Finishing our feature

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

Picture of coffee

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

WIP commit New Stuff

Slide 130

Slide 130 text

WIP commit New Stuff

Slide 131

Slide 131 text

WIP commit New Stuff other stuff

Slide 132

Slide 132 text

Interactive Stage

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

$ git commit Commit the current changes

Slide 146

Slide 146 text

$ git add -A $ git commit Commit everything else

Slide 147

Slide 147 text

$ git add -A $ git commit Commit everything else

Slide 148

Slide 148 text

WIP commit New Stuff other stuff

Slide 149

Slide 149 text

other stuff New Stuff

Slide 150

Slide 150 text

Interactive Rebase

Slide 151

Slide 151 text

$ git rebase -i HEAD~3 Interactive Rebase

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

other stuff New Stuff

Slide 158

Slide 158 text

Scenario: Loosing a commit

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

$ git rebase -i HEAD~10

Slide 161

Slide 161 text

λ 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".

Slide 162

Slide 162 text

$ git rebase --continue

Slide 163

Slide 163 text

λ 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

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

Picture of you crying

Slide 166

Slide 166 text

Your work was lost in a conflict

Slide 167

Slide 167 text

Don’t Panic

Slide 168

Slide 168 text

$ git reflog

Slide 169

Slide 169 text

The reflog is all of your repos history

Slide 170

Slide 170 text

$ git reflog

Slide 171

Slide 171 text

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

Slide 172

Slide 172 text

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

Slide 173

Slide 173 text

039d19a

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

Conclusion

Slide 177

Slide 177 text

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

Slide 178

Slide 178 text

CLI

Slide 179

Slide 179 text

Tools

Slide 180

Slide 180 text

Branching

Slide 181

Slide 181 text

Rebase

Slide 182

Slide 182 text

Reflog

Slide 183

Slide 183 text

Don’t Panic

Slide 184

Slide 184 text

Thanks! Chris Keathley @ChrisKeathley [email protected]