42
Git Power Routines NICOLA PAOLUCCI DEVELOPER INSTIGATOR ATLASSIAN @DURDN Master the art and practice of DVCS

Git Power Routines

Embed Size (px)

Citation preview

Page 1: Git Power Routines

Git Power Routines

NICOLA PAOLUCCI • DEVELOPER INSTIGATOR • ATLASSIAN • @DURDN

Master the art and practice of DVCS

Page 2: Git Power Routines

@durdn

R E M O V E B I N A R I E S

Follow along, solve real world daily version control situations

C O N F L I C T R E S O L U T I O N

M E R G E S T R AT E G I E S

I N T E R A C T I V E C O M M I T S

A M E N D A N D R E B A S E

Page 3: Git Power Routines

A D V E N T U R E AWA I T S

Follow along the adventure

This session won’t be interactive but you can follow along and try to beat me to tasks by

cloning this repository

git clone \ [email protected]:nicolapaolucci/starwars-summary.git

Page 4: Git Power Routines

@durdn

About the sample project

Page 5: Git Power Routines

A D V E N T U R E AWA I T S

House keeping [1]Get a nice prompt: Liquid prompt is awesome

http://bit.do/liquid-prompt

Page 6: Git Power Routines

A D V E N T U R E AWA I T S

House keeping [2]

If you are creating a new repository, start with an empty commit and tag it

git commit -m"[empty] Initial commit" --allow-empty

git tag initial <first-sha-1> -m"Tag initial commit"

Page 7: Git Power Routines

A D V E N T U R E AWA I T S

House keeping [3]

To speed up the typing I have created a few aliases that we will re-use throughout

ls = log --decorate --oneline

ll = log --decorate --numstat

http://bit.do/gitconfig

Page 8: Git Power Routines

@durdn

Common and not so common un-doings and re-doings

Amending and Re-basing

Page 9: Git Power Routines

Credit: Emma Jane Hogbin Westby @emmajanehw

Page 10: Git Power Routines

Credit: Emma Jane Hogbin Westby @emmajanehw

Page 11: Git Power Routines

Obvious!

Page 12: Git Power Routines

T H E O RY

Always think of these

Work directory

Which tree does my command affect?

Index or Staging area

Local repository (.git) Remotes

Page 13: Git Power Routines

U N D O I N G S

Memorise or alias this to amend quickly

Amend the last commit with everything I have here uncommitted and new

caa = commit -a --amend -C HEAD

Page 14: Git Power Routines

T H E O RY

Default git reset is of type mixed…

Work directory

Which tree does my command affect?

Index or Staging area

Local repository (.git)

HEAD

master

git reset HEAD~

HEADHEAD

HEAD

Page 15: Git Power Routines

changes

T H E O RY

Default git reset is a mixed reset…

Work directory

Which tree does my command affect?

Index or Staging area

Local repository (.git)

HEADmaster

git reset HEAD~

HEADHEAD

Page 16: Git Power Routines

@durdn

Ancestry referencesYou can specify commits via their ancestry.

The more common examples are

HEAD^ = the parent of commit HEAD

HEAD~2 = traverses the first parent 2 times, finds the grand parent

Page 17: Git Power Routines

Now let’s change a commit in the past

Page 18: Git Power Routines

@durdn

What is a rebase?It’s a way to replay commits, one

by one, on top of a branch

master

feature

Page 19: Git Power Routines

@durdn

What is an --interactive rebase?

Helps you clean up your private branches before publishing them

reword fixup

pick squash

edit exec

Page 20: Git Power Routines

For complex history cleanup use

git filter-branch or BFG

Page 21: Git Power Routines

U N D O I N G S

BFG Repo-Cleaner is a nice optionhttp://bit.do/bfg

Page 22: Git Power Routines

@durdn

Let’s learn to solve conflictsBelieve me, it can be done without tears and sweat.

Page 23: Git Power Routines

C O N F L I C T R E S O L U T I O N

A word on terminology

Current checked out branch

--ours

What do ours and theirs mean when solving conflicts?

Commit coming in (i.e. via merge)

--theirs

Page 24: Git Power Routines

C O N F L I C T R E S O L U T I O N

Basics for easy conflict resolutionThe common commands are:

$ git checkout --ours/--theirs <file> Check back out our own/their own version of the file

$ git add <file> Add the change to the index will resolve the conflict

Page 25: Git Power Routines

C O N F L I C T R E S O L U T I O N

Aliases for easy conflict resolutionAdd these to [alias] in .gitconfig:

ours = "!f() { \ \ }; f" git checkout --ours $@ && git add $@;

Page 26: Git Power Routines

@durdn

Embrace useful merge strategiesMerging the right way can go a long way…

Page 27: Git Power Routines

@durdn

What is a merge?

Merge commit

master

feature

merges keep the context of the feature’s commits

feature

master

Page 28: Git Power Routines

tree f362c42032aff677c1a09c3f070454df5b411239 parent 49a906f5722ad446a131778cea52e3fda331b706 parent bd1174cd0f30fe9be9efdd41dcd56256340f230e author Marcus Bertrand <[email protected]> 1409002123 -0700 committer Marcus Bertrand <[email protected]> 1409002123 -0700

Merge branch 'foo/mybranch'

.git/objects/36/80d8c8fd182f97cb0e75045e2fed5c7b7613ed

commit

Anatomy of a merge

Page 29: Git Power Routines

@durdn

Let’s talk about merge strategies!

git has breadth of choice on how to merge changes!

recursiveresolve octopus subtreeours yours?

Page 30: Git Power Routines

@durdn

merge strategy: ours

master

feature

Records a merge but skips incoming changes

IGNORE!

Page 31: Git Power Routines

strategy --ours is useful to promote recent

branches to master

Page 32: Git Power Routines

@durdn

feature

What is a fast-forward merge?

master

It will just shift the HEAD tag

feature

master

Page 33: Git Power Routines

@durdn

What is a fast-forward merge?

master

It will just shift the HEAD tag

feature

master

Page 34: Git Power Routines

Octopus merge is nice for staging

servers.

Page 35: Git Power Routines

U N D O I N G S

Octopus Merge can be very usefulhttp://bit.do/git-octopus

Page 36: Git Power Routines

U N D O I N G S

Octopus Merge can be very useful

Page 37: Git Power Routines

@durdn

Interactive add (and commit)Splitting commits semantically in flight!

Page 38: Git Power Routines

Stage this hunk [y,n,q,a,d,/,s,e,?]? ? y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help

Page 39: Git Power Routines

@durdn

Switch context at willGit stash power techniques

Page 40: Git Power Routines

@durdn

git stash is awesomeIt’s a way to temporarily store your

unsaved work on a stack of patches

stash@{0}

stash@{1}

stash@{2}

stash@{3}

git stash save

Page 41: Git Power Routines

@durdn

Page 42: Git Power Routines

Thank you!

NICOLA PAOLUCCI • DEVELOPER INSTIGATOR • ATLASSIAN • @DURDN

Twitter: @durdn