+ All Categories
Home > Engineering > Git-ing out of your git messes

Git-ing out of your git messes

Date post: 16-Apr-2017
Category:
Upload: katrina-sylor-miller
View: 1,019 times
Download: 0 times
Share this document with a friend
116
Git-ing out of your Git messes Katie Sylor-Miller Senior Software Engineer, Etsy ohshitgit.com
Transcript
Page 1: Git-ing out of  your git messes

Git-ing out of your Git messes

Katie Sylor-MillerSenior Software Engineer, Etsy

ohshitgit.com

Page 2: Git-ing out of  your git messes
Page 3: Git-ing out of  your git messes

DON’T GIT INTO A MESS IN THE FIRST PLACE

Page 4: Git-ing out of  your git messes

Fundamentalscommits, branches, HEAD & environments

Page 5: Git-ing out of  your git messes

Fundamentals: Commits

Page 6: Git-ing out of  your git messes

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

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

Commit hashes

a4df41a8045877d50396d00113598e47f6ad10ef

aec561108fd86412c2a9083d7d95ed1668d2f4e4

6dab6a712177cf2dd2cf8b79a2cee24351be60eb

1a3531222242241153ac8a76b752d5f525a99d2c

912bde5d4e5e962269ddff87da83cc5ce55e75d0

Page 9: Git-ing out of  your git messes

Fun fact: commit hash abbreviations

a4df41a

aec5611

6dab6a7

1a35312

912bde5

Page 10: Git-ing out of  your git messes

Fundamentals: Branches

Page 11: Git-ing out of  your git messes

● 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

Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5

Page 13: Git-ing out of  your git messes

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

Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5

parent child

Page 15: Git-ing out of  your git messes

Mental model: a linked list of commitsa4df41aaec56116dab6a71a35312 912bde5

Page 16: Git-ing out of  your git messes

Branches are a reference to a commit

master

a4df41aaec56116dab6a71a35312 912bde5

Page 17: Git-ing out of  your git messes

git branch new-branch

Create a new branch

Page 18: Git-ing out of  your git messes

Branches are a reference to a commit

master

a4df41aaec56116dab6a71a35312 912bde5

new-branch

Page 19: Git-ing out of  your git messes

Fundamentals: HEAD

Page 20: Git-ing out of  your git messes

HEAD points to currently checked-out branch

master

new-branch

a4df41aaec5611 912bde56dab6a7

HEAD

Page 21: Git-ing out of  your git messes

Check out a branch git checkout new-branch

Page 22: Git-ing out of  your git messes

HEAD points to currently checked-out branch

master

new-branch

a4df41aaec5611 912bde56dab6a7

HEAD

Page 23: Git-ing out of  your git messes

A new commit’s parent is the HEAD

master

new-branch

a4df41aaec5611 912bde5 1668d2f6dab6a7

HEAD

Page 24: Git-ing out of  your git messes

master

new-branch

a4df41aaec5611 912bde5

1668d2f

ca53f4f 6aac7b2

Branches are a linked list treeHEAD

Page 25: Git-ing out of  your git messes

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

Fundamentals: Remote vs. Local

Page 27: Git-ing out of  your git messes

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

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

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

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

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

HistoryViewing and changing history

Page 33: Git-ing out of  your git messes

Viewing history

Page 34: Git-ing out of  your git messes

See the history of a

branch git log

Page 35: Git-ing out of  your git messes

See the history of a

branchgit log

git log

Page 36: Git-ing out of  your git messes

See the history of the HEAD git reflog

Page 37: Git-ing out of  your git messes

See the history of the HEAD

git reflog

git reflog

Page 38: Git-ing out of  your git messes

Moving backwards in history

Page 39: Git-ing out of  your git messes

CheckoutGo back to a specific

point in time

git checkout <commit hash>

Page 40: Git-ing out of  your git messes

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

Fixing messesReset & revert

Page 42: Git-ing out of  your git messes

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

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

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

git reset --hardPro tipClear out your staging

area and workspace

Page 46: Git-ing out of  your git messes

git revert <commit hash># orgit revert HEAD~X

RevertUndo a public commit

Page 47: Git-ing out of  your git messes

● 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

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

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

WorkflowCommitting & branching

Page 51: Git-ing out of  your git messes

A simple workflow

Page 52: Git-ing out of  your git messes

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

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

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

master

master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 56: Git-ing out of  your git messes

git push

Send changes to

remote

Page 57: Git-ing out of  your git messes

master

git pushmaster

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 58: Git-ing out of  your git messes

master master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 59: Git-ing out of  your git messes

But… it’s usually not that simple

Page 60: Git-ing out of  your git messes

master

master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 61: Git-ing out of  your git messes

Avoiding messesStay up-to-date

Page 62: Git-ing out of  your git messes

master

master

origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 63: Git-ing out of  your git messes

git fetch origin

Update local tracking branch

Page 64: Git-ing out of  your git messes

master

master

git fetch

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

origin/master

Page 65: Git-ing out of  your git messes

git merge origin/masterMerge

Page 66: Git-ing out of  your git messes

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

master

master

origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 68: Git-ing out of  your git messes

git pull# git fetch && git merge

PullDo a fetch & merge at the

same time

Page 69: Git-ing out of  your git messes

master

master

origin/master

git pull

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 70: Git-ing out of  your git messes

git push

Sync changes to

remote

Page 71: Git-ing out of  your git messes

master

git push

master origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 72: Git-ing out of  your git messes

mastermaster origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 73: Git-ing out of  your git messes

Avoiding messesRebase all the things

Page 74: Git-ing out of  your git messes

master

master

origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 75: Git-ing out of  your git messes

git rebase origin/masterRebase

Page 76: Git-ing out of  your git messes

master origin/master

master

Our commit

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 77: Git-ing out of  your git messes

masterorigin/master

master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 78: Git-ing out of  your git messes

git pull --rebase# git fetch && git rebase

Pull with Rebase

Page 79: Git-ing out of  your git messes

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

masterorigin/master

master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 81: Git-ing out of  your git messes

git push

Sync changes to

remote

Page 82: Git-ing out of  your git messes

masterorigin/master

mastergit push

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 83: Git-ing out of  your git messes

master origin/master master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 84: Git-ing out of  your git messes

Avoiding MessesAlways Be Committing

Page 85: Git-ing out of  your git messes

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

Problem: Committing the wrong thing

Page 87: Git-ing out of  your git messes

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

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

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

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

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

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

Avoiding messesAlways Be Branching

Page 94: Git-ing out of  your git messes

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

Problem: Staying up to date

Page 96: Git-ing out of  your git messes

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

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

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

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
Page 101: Git-ing out of  your git messes

DON’T PANIC

Page 102: Git-ing out of  your git messes

master

feature-branch

origin/master

Remote/Origin Local Staging/Index Workspace Stash

Your machinegithub

Page 103: Git-ing out of  your git messes

git checkout master && git merge --squash feature-branch

Merging back to master

*controversial opinion*

Page 104: Git-ing out of  your git messes

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

● 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

Avoiding messesResolving conflicts

Page 107: Git-ing out of  your git messes

● 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

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

The problem: conflict markers

Page 110: Git-ing out of  your git messes

Fix the problem

Check for remaining conflict markers before

committing

git diff --check

Page 111: Git-ing out of  your git messes

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

If all else fails

Abort! abort!

git merge --abort# orgit rebase --abort

Page 113: Git-ing out of  your git messes

That’s it! Easy, right?

Page 114: Git-ing out of  your git messes

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

● 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

Thank you!@ksylor && @ohshitgit


Recommended