git-ing out of your git messes
TRANSCRIPT
![Page 1: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/1.jpg)
Git-ing out of your Git messes
Katie Sylor-MillerSenior Software Engineer, Etsy
ohshitgit.com
![Page 2: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/2.jpg)
![Page 3: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/3.jpg)
DON’T GIT INTO A MESS IN THE FIRST PLACE
![Page 4: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/4.jpg)
Fundamentalscommits, branches, HEAD & environments
![Page 5: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/5.jpg)
Fundamentals: Commits
![Page 6: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/6.jpg)
Each commit contains a few pieces of information:
● A snapshot of the entire repo
● Who made this change
● When this change was made
● A message describing the commit
● A pointer to the previous commit(This is a bit of an over simplification, for a more detailed explanation, see: http://blog.thoughtram.io/git/2014/11/18/the-anatomy-of-a-git-commit.html)
What’s in a commit
![Page 7: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/7.jpg)
Each commit contains a few pieces of information:
● A snapshot of the entire repo
● Who made this change
● When this change was made
● A message describing the commit
● A pointer to the previous commit(This is a bit of an over-simplification, for a more detailed explanation, see: http://blog.thoughtram.io/git/2014/11/18/the-anatomy-of-a-git-commit.html)
What’s in a commit
SHA-1Unique40-charHash
![Page 8: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/8.jpg)
Commit hashes
a4df41a8045877d50396d00113598e47f6ad10ef
aec561108fd86412c2a9083d7d95ed1668d2f4e4
6dab6a712177cf2dd2cf8b79a2cee24351be60eb
1a3531222242241153ac8a76b752d5f525a99d2c
912bde5d4e5e962269ddff87da83cc5ce55e75d0
![Page 9: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/9.jpg)
Fun fact: commit hash abbreviations
a4df41a
aec5611
6dab6a7
1a35312
912bde5
![Page 10: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/10.jpg)
Fundamentals: Branches
![Page 11: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/11.jpg)
● Each git repository starts out with a single branch, called master.
● There can be multiple branches of each repo.● Each branch is essentially a copy of the master branch
and all of it’s history
● Branches are cheap and easy (unlike TFS or SVN), so use them as much as you want!
Branches: the connection between commits
![Page 12: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/12.jpg)
Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5
![Page 13: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/13.jpg)
Each commit contains a few pieces of information:● A snapshot of the entire repository● Who made this change● When this change was made● A message describing the commit● A pointer to the previous commit
Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5
![Page 14: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/14.jpg)
Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5
parent child
![Page 15: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/15.jpg)
Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5
![Page 16: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/16.jpg)
Branches are a reference to a commit
master
a4df41aaec56116dab6a71a35312 912bde5
![Page 17: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/17.jpg)
git branch new-branch
Create a new branch
![Page 18: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/18.jpg)
Branches are a reference to a commit
master
a4df41aaec56116dab6a71a35312 912bde5
new-branch
![Page 19: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/19.jpg)
Fundamentals: HEAD
![Page 20: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/20.jpg)
HEAD points to currently checked-out branch
master
new-branch
a4df41aaec5611 912bde56dab6a7
HEAD
![Page 21: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/21.jpg)
Check out a branch git checkout new-branch
![Page 22: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/22.jpg)
HEAD points to currently checked-out branch
master
new-branch
a4df41aaec5611 912bde56dab6a7
HEAD
![Page 23: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/23.jpg)
A new commit’s parent is the HEAD
master
new-branch
a4df41aaec5611 912bde5 1668d2f6dab6a7
HEAD
![Page 24: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/24.jpg)
master
new-branch
a4df41aaec5611 912bde5
1668d2f
ca53f4f 6aac7b2
Branches are a linked list treeHEAD
![Page 25: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/25.jpg)
Branches are a linked list tree directed acyclic graph
https://en.wikipedia.org/wiki/Directed_acyclic_graph
![Page 26: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/26.jpg)
Fundamentals: Remote vs. Local
![Page 27: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/27.jpg)
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
Central server where shared git repositories are stored
--
Remotes typically are “bare” - you can’t directly modify them
![Page 28: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/28.jpg)
Your local copy of the remote git repository
--
Contains the entire history and all branches of a remote repo
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 29: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/29.jpg)
Snapshot of changes to the current branch that you want to commit
--
Is a copy of all of the files in the repo, not just the changed files
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 30: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/30.jpg)
Where changes to files are made
--
Analogous to the physical directory where files are stored on disk
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 31: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/31.jpg)
A place to store changes to files that you aren’t ready to commit yet--Aka “shelving” changes for later
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 32: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/32.jpg)
HistoryViewing and changing history
![Page 33: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/33.jpg)
Viewing history
![Page 34: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/34.jpg)
See the history of a
branch git log
![Page 35: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/35.jpg)
See the history of a
branchgit log
git log
![Page 36: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/36.jpg)
See the history of the HEAD git reflog
![Page 37: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/37.jpg)
See the history of the HEAD
git reflog
git reflog
![Page 38: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/38.jpg)
Moving backwards in history
![Page 39: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/39.jpg)
CheckoutGo back to a specific
point in time
git checkout <commit hash>
![Page 40: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/40.jpg)
Aside: Detached HEAD
Detached HEAD means that HEAD is not a symbolic reference anymore, therefore new commits will not be part of history. Happens when you:
● Checkout a commit that is not the tip of a branch, or
● Checkout a remote tracking branch
Fix it by:
● Checking out a branch, or
● Create a new branch from this state
![Page 41: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/41.jpg)
Fixing messesReset & revert
![Page 42: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/42.jpg)
git reset HEAD@{x}# orgit reset HEAD~x# orgit reset <commit hash>
ResetGo back to a previous
point in time
![Page 43: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/43.jpg)
git reset --soft HEAD~git reset --mixed HEAD~git reset --hard HEAD~
Three types of resetting
![Page 44: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/44.jpg)
soft
Takes you back in history, and
leaves your changes in staging
Takes you back in history, and discards those
changes
hardmixed
(default) takes you back in history, and leaves your
changes in the workspace
![Page 45: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/45.jpg)
git reset --hardPro tipClear out your staging
area and workspace
![Page 46: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/46.jpg)
git revert <commit hash># orgit revert HEAD~X
RevertUndo a public commit
![Page 47: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/47.jpg)
● Pass in the identifier(s) of specific commits
● Git creates a new commit that undoes the work of the specified commit.
● You can revert a commit in the middle of other commits, but if a later commit modifies the same file, you will need to resolve that conflict.
Reverting commits
![Page 48: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/48.jpg)
Revert multiple commits
You can use either HEAD~ references, or commit
hashes
# rangegit revert HEAD~3..HEAD# or list newest->oldestgit revert HEAD~2 HEAD~3 HEAD~4
![Page 49: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/49.jpg)
Revert without
auto-commitIn case you want to
double-check the revert
git revert --no-commit <commit or range or list># leaves changes staged# for manual commit
![Page 50: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/50.jpg)
WorkflowCommitting & branching
![Page 51: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/51.jpg)
A simple workflow
![Page 52: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/52.jpg)
master master
baz.php
foo.php
bar.php
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 53: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/53.jpg)
master master
baz.php
git add
foo.php
bar.php
baz.php
foo.php
bar.php
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 54: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/54.jpg)
master HEADmaster
git commit
baz.php
foo.php
bar.php
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 55: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/55.jpg)
master
master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 56: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/56.jpg)
git push
Send changes to
remote
![Page 57: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/57.jpg)
master
git pushmaster
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 58: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/58.jpg)
master master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 59: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/59.jpg)
But… it’s usually not that simple
![Page 60: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/60.jpg)
master
master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 61: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/61.jpg)
Avoiding messesStay up-to-date
![Page 62: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/62.jpg)
master
master
origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 63: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/63.jpg)
git fetch origin
Update local tracking branch
![Page 64: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/64.jpg)
master
master
git fetch
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
origin/master
![Page 65: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/65.jpg)
git merge origin/masterMerge
![Page 66: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/66.jpg)
master
master
origin/master
New merge commit
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 67: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/67.jpg)
master
master
origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 68: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/68.jpg)
git pull# git fetch && git merge
PullDo a fetch & merge at the
same time
![Page 69: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/69.jpg)
master
master
origin/master
git pull
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 70: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/70.jpg)
git push
Sync changes to
remote
![Page 71: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/71.jpg)
master
git push
master origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 72: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/72.jpg)
mastermaster origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 73: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/73.jpg)
Avoiding messesRebase all the things
![Page 74: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/74.jpg)
master
master
origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 75: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/75.jpg)
git rebase origin/masterRebase
![Page 76: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/76.jpg)
master origin/master
master
Our commit
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 77: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/77.jpg)
masterorigin/master
master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 78: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/78.jpg)
git pull --rebase# git fetch && git rebase
Pull with Rebase
![Page 79: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/79.jpg)
git config --global alias.rpull ‘pull --rebase’Pro tip
Add rpull as an alias for git pull --rebase
![Page 80: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/80.jpg)
masterorigin/master
master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 81: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/81.jpg)
git push
Sync changes to
remote
![Page 82: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/82.jpg)
masterorigin/master
mastergit push
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 83: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/83.jpg)
master origin/master master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 84: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/84.jpg)
Avoiding MessesAlways Be Committing
![Page 85: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/85.jpg)
Why lots of commits are better
● Commits are cheap and easy
● Save progress over time - easier to go back
● Smaller diffs are easier to reason about
● Less chance of committing the wrong thing when you are reviewing small changelists
![Page 86: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/86.jpg)
Problem: Committing the wrong thing
![Page 87: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/87.jpg)
Avoid the problem: Committing the wrong thing
● Set up your command line to show you what branch you are on (https://github.com/jimeh/git-aware-prompt)
● Be careful about what files you edit - use .gitignore to your advantage
● git status is your BFF
● Understand how staging works (subsequent changes to a file are not reflected)
![Page 88: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/88.jpg)
Fix the problem
Entering the wrong commit message
# make sure nothing is in staginggit commit -amend# follow prompts to change # the commit message
![Page 89: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/89.jpg)
Fix the problem
Forgetting to commit a file
git add filenamegit commit -amend# follow prompts to change # the commit message
![Page 90: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/90.jpg)
Fix the problem
Committing the wrong file
# undo your last commit# but leave the changes in staginggit reset HEAD~# unstage the filegit reset HEAD filename# re-do commitgit commit -m “Commit message”
![Page 91: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/91.jpg)
Fix the problem
Committing to the wrong branch (version one)
# undo the last commit, but leave the changes availablegit reset HEAD~ --softgit stash# move to the correct # branchgit checkout name-of-the-correct-branchgit stash popgit add . # or add individual filesgit commit -m "your message here"# now your changes are on the correct branch
![Page 92: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/92.jpg)
Fix the problem
Committing to the wrong branch (version two)
git checkout name-of-the-correct-branch# grab the last commit to mastergit cherry-pick master# delete it from mastergit checkout mastergit reset HEAD~ --hard
![Page 93: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/93.jpg)
Avoiding messesAlways Be Branching
![Page 94: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/94.jpg)
Why a feature branch workflow is better
● Create a new branch of master, do your work on that branch. When you are ready, merge your changes back into master
● Safety net: you aren’t changing master directly until your feature is ready for prime time
● Allows you to switch between tasks/manage unrelated changes
● Preserve the history of larger features or long-term work and share with a team
![Page 95: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/95.jpg)
Problem: Staying up to date
![Page 96: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/96.jpg)
MERGE REBASE
● Adds a new commit to your feature branch.
● You are resolving potential conflicts created by other people’s code.
● Replays your commits on top of the latest of master.
● You are resolving potential conflicts created by your own code.
Avoid the problem: rebase vs. merge
![Page 97: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/97.jpg)
Rebasing gotchas & caveats
● Each commit is applied as a separate patch, so you might need to resolve conflicts for each commit :( :( :(
● Don’t change the public history of a branch or you’re gonna have a bad time.
● Some people like to keep commit history of branches. Discuss with your team which is preferred!
![Page 98: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/98.jpg)
git rebase -i HEAD~x
Avoid the problem
Combine commits so you have fewer conflicts to deal with when rebasing
![Page 99: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/99.jpg)
master
feature-branch
origin/master
git rebase-i HEAD~2
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 100: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/100.jpg)
![Page 101: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/101.jpg)
DON’T PANIC
![Page 102: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/102.jpg)
master
feature-branch
origin/master
Remote/Origin Local Staging/Index Workspace Stash
Your machinegithub
![Page 103: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/103.jpg)
git checkout master && git merge --squash feature-branch
Merging back to master
*controversial opinion*
![Page 104: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/104.jpg)
Remote (Origin) Local Staging/Index Workspace Stash
master
Your VMgithub.etsycorp.com
Etsyweb
Etsyweb
feature-branch
master
![Page 105: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/105.jpg)
● Combines all of the commits in your feature branch into a single changeset
● Leaves you in a state where the changes are not committed, you need to make the final commit
● Lose historical connection to the feature branch
Squash merging
![Page 106: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/106.jpg)
Avoiding messesResolving conflicts
![Page 107: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/107.jpg)
● Communication with teammates
● Update your local master and origin/master all.the.time.
● Periodically squash commits to reduce the number of commit conflicts to resolve (esp. When rebasing)
Avoid conflicts in the first place
![Page 108: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/108.jpg)
DO DON’T
● Merge feature-branch into master
● Rebase feature-branch against master
● Merge master into feature-branch
● Rebase master against feature-branch
Avoid conflicts: merge & rebase in the right direction
![Page 109: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/109.jpg)
The problem: conflict markers
![Page 110: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/110.jpg)
Fix the problem
Check for remaining conflict markers before
committing
git diff --check
![Page 111: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/111.jpg)
Pro tipUse git mergetool command to open a GUI to help
resolve conflicts. HIGHLY RECOMMEND any Jetbrains IDE, also meld looks good
git config merge.tool <toolname>
![Page 112: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/112.jpg)
If all else fails
Abort! abort!
git merge --abort# orgit rebase --abort
![Page 113: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/113.jpg)
That’s it! Easy, right?
![Page 114: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/114.jpg)
Recap: Avoiding messes
● Understand the fundamentals (commits, branches, history, environments, workflow)
● Use tools to help you work smarter (cmd line formatting mergetool, etc.)
● Always be committing & branching
● Rebase & merge in the right direction
● DON’T PANIC! Everything is fixable (one way or another)
![Page 115: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/115.jpg)
● Visual git cheatsheethttp://ndpsoftware.com/git-cheatsheet.html
● Oh shit, git! http://ohshitgit.com
● Atlassian git tutorial (esp. the advanced tutorials)https://www.atlassian.com/git/
● Git for Humans bookhttps://abookapart.com/products/git-for-humans
Useful links
![Page 116: Git-ing out of your git messes](https://reader035.vdocument.in/reader035/viewer/2022062401/5870c5851a28ab0b4a8b8203/html5/thumbnails/116.jpg)
Thank you!@ksylor && @ohshitgit