Merging in git

getting your hands dirty

Andy Balaam
artificialworlds.net/blog

Contents

Diverging branches



Cherry-picking

take what you want

Cherry-picking


We want a bug fix that went into trunk:


Cherry-picking

$ cd sayings
$ gitk --all

Cherry-picking

Find the commit we want:

$ git checkout master
$ git log
...
commit 80e837528448ac65196fe6096477ae148907cb7b
Author: Andy Balaam >andybalaam at artificialworlds.net>
Date:   Fri Jul 5 09:49:10 2013 +0100

    Fix missing name in surprise expression
...

Cherry-picking

Cherry-pick it:

$ git checkout release-0.2
Switched to branch 'release-0.2'

$ git cherry-pick 80e837528448ac65196fe6096477ae148907cb7b
[release-0.2 74acfc9] Fix missing name in surprise expression
 1 file changed, 1 insertion(+), 1 deletion(-)

Cherry-picking

What happened?

$ git checkout master
$ git log
...
commit 74acfc9de12891ef4b390cc82dd4a50cdc7c6cce
Author: Andy Balaam 
Date:   Fri Jul 5 09:49:10 2013 +0100

    Fix missing name in surprise expression

It's a new, different commit

Merging

eat the pain

Merging

Merging

Tell it to merge:

$ git branch
* master
  release-0.1
  release-0.2

$ git merge release-0.2
Auto-merging southern.txt
CONFLICT (content): Merge conflict in southern.txt
Automatic merge failed; fix conflicts and then commit the result.

Merging

Where are we?

$ git status
# On branch master
# Unmerged paths:
#   (use "git add/rm ..." as appropriate to mark resolution)
#
#   both modified:      southern.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

Merging

To complete merging, we must:

Important: don't do anything else until we're done!

Merging

southern.txt
Gor blimey Charlie
Oi oi
Awight lads
<<<<<<< HEAD
Where to guvnor?
Me old China
Apples and pears
=======
'Ow's it goin'?
Where to guv'nr?
Me ol' China

>>>>>>> release-0.2

Merging

$ git mergetool 
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 vimdiff emerge
Merging:
southern.txt

Normal merge conflict for 'southern.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (kdiff3): 

Merging

Merging

$ # mergetool did the "add" for us
$ git commit
...

Merging

$ gitk --all

Merging

Alternatively, abort a merge:

$ git merge --abort

Rebase

conceal the pain

Rebase

$ git checkout master
$ git rebase release-0.2
First, rewinding head to replay your work on top of it...
Applying: Northern expression of affectionate surprise
Applying: Scottish sayings file.
Applying: Stating the obvious for Scots
Applying: Fix missing name in surprise expression
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: Add a contemptuous scots saying
Applying: Fruit-based reference to stairs
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging southern.txt
CONFLICT (content): Merge conflict in southern.txt
Failed to merge in the changes.
Patch failed at 0006 Fruit-based reference to stairs

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To check out the original branch and stop rebasing run "git rebase --abort".

Rebase

You can abort:

$ git rebase --abort

Rebase

$ git mergetool
...
$ git rebase --continue
...
$ git mergetool
...
$ git rebase --continue

Rebase

$ gitk --all

Rebase

The difference:

More info

Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net