Search This Blog

Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Monday, September 17, 2012



With svn we had to parse mergeinfo files and were constantly trying to find out what merges were and weren't on which branch.  Git has powerful tools to help us do the same thing, and git maintains them correctly.  Because any branch is just a linked list of patches there are low level git tools that tell you exactly which patches are where.

Let's look at an example.  First, let's set up some repositories:

mkdir merge_example
cd merge_example
git init
cat > README << EOF
read me
EOF
git add README
git commit -m "Initial checkin"
git checkout -b branch1
cat > file2 << EOF
line one
EOF
git add file2
git commit -m file2_line1
cat >> file2 << EOF
line two
EOF
git commit -m file2_line2 -a
git checkout master
cat > file3 << EOF
line one
EOF
git add file3
git commit -m file3
cat > file4 << EOF
line one
EOF
git add file4
git commit -m file4

Now we can see what is on master and not on branch1
git log master..branch1
And we can see what's on branch1 and not on master
git log branch1..master

Pretty easy there.  Let's try doing a merge from master onto branch1.  Pretty easy, only the one checkin.

git checkout branch1
git merge master

Note, that was not a rebase, so it did not fast forward.  In this case we run git log master..branch1 and see that there is one more commit on branch1 than on master.

Now when we merge back to master git will handle things for us

git checkout master
git merge branch1

In this case we fast forwarded and had no extra merge.  That's because everything on master was on branch1 and the missing patches could just be applied.

OK, that was easy, but could it have been easier?  YES!

Delete everything and run the first script again:

Now rebase instead of merge:

git checkout branch1
git rebase master

Note, no merge was needed!  No bonus merge message.  When we merge back

git checkout master
git rebase branch1

blame!  Everything is there, no merging was even needed.

Could it have been more complicated?  Oh yeah, it's source control, it can always be harder.  It's called cherry picking.

Blow away everything and find the commit for file 4 with git log branch1..master.  In my case it's 7b86e70280887a711ee5b9be36d60d94d8ee17cc

Now let's just merge that ONE thing.

git checkout branch1
git cherry-pick 7b86e70280887a711ee5b9be36d60d94d8ee17cc

Now when you run git log branch1..master and git log master..branch1 you should see that there is a file4 on both master and branch1 with different commits.  Think back to what a branch is.  It's a pointer to a commit with a chain all the way back to origin.  file4 on master points to file3.  file4 on branch1 points to file2_line2.  Those are different commits and are tracked as different.  Run gitk --all and you can see what happened.

So what happens if I pull everything from branch1 onto master?

If you rebase you end up just reordering patches to end up at the same end result.  Git is pretty smart about being able to do that.

If on the other hand you had merged you end up with file4 being added twice!  Try it

git checkout master
git merge branch1
git log

So what's the lesson?  Always rebase, never merge.









How do I fix my merge tree to look nice?

Make sure you did the prior exercise so you have merge_client.  Clone the merge_client so you can play around and if necessary delete everything and start over.

git clone merge_client fix_merge_client
cd fix_merge_client

Before we rebase we want to find the commit number for our Initial README change.

git log --oneline

In my log it looks like this:


575c05f Merge branch 'master' of /Users/justsomeguy/google/projects/gitmigration/tmp/
dc7347b client1-README2
1dcfd87 Merge branch 'master' of /Users/justsomeguy/google/projects/gitmigration/tmp/
33d11e5 client1-readme
3526271 merge-readme
3984243 Initial README


Now we are going to rebase it to put our commits in order and remove the bonus merges we don't want.

git rebase -i --onto 3984243 HEAD~3

That means we are going to look at the last 3 real commits (non-merge) from HEAD, in other words client1-README2, merge_client, and client1

In the editor move the pick for merge_client to the end.  This will reorder the patches to give us the nice streamlined no branch we saw in rebasing.

git helpfully responds with this message:

The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

   git commit --allow-empty

Otherwise, please use 'git reset'
# Not currently on any branch.
nothing to commit (working directory clean)
Could not apply 9dc94e6... client1

This is a confusing message because what it's saying is that it doesn't need to do anything, which is actually what you want.  Just continue:

git rebase --continue

This leads to a merge conflict:

error: could not apply 71f6c62... merge_client

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".
Could not apply 71f6c62... merge_client


We expected that, after all we know it's a conflict.  Go ahead and merge it like before with:

git mergetool
git rebase --continue

You should get a request for a git commit message, the default should be fine.

verify the changes now look clean with

git log --oneline

Notice the extra merge commits are now out of the HEAD and the merge order is consistent with rebase_client


Todays exercise is git rebase vs git merge.  Often you will hear "always rebase never merge" and have no idea why people say don't merge when you are still merging.  This exercise will show the difference between the two and why rebase is your friend.

First the exercise.

# create an empty server

mkdir server
cd server
git init --bare
cd ..

# make a client and do an initial checkin
git clone server client1
cd client1
touch README
git add README
git commit -m "Initial README"
git push --all
cd ..

# make a two new clients
git clone server merge_client
git clone server rebase_client

# create a conflict
# in each of the three clients edit the first line of README to say the name of the client
# then commit the change

cd client1
cat > README <<EOF
client1
EOF
git commit -a -m "client1-readme"
cd ..


cd merge_client
cat > README <<EOF
merge_client
EOF
git commit -a -m "merge-readme"
cd ..

cd rebase_client
cat > README <<EOF
rebase_client
EOF
git commit -a -m "rebase-readme"
cd ..



# in client1 push the change

cd client1 git push
cd ..

# in merge_client do a merge without a rebase

cd merge_client
git pull
git mergetool
git commit -a
git log
cd ..

# in rebase_client do a rebase.  Rebases need you to fetch objects first.

cd rebase_client
git fetch
git rebase
git mergetool
git rebase --continue
git log
cd ..

Notice the difference in logs between rebase_client and merge_client?  What happened was rebase updated your tree under your changes then reapplied your changes.  All of your changes are modified to be as if you had done them against the tip of the tree.  Do a git show in both trees.  See what happened in merge_client and compare that to what happened in rebase_client?

It gets better, even if you don't have a merge conflict you want to rebase.

cd client1
touch README2
git add README2
git commit -m "client1-README2"
git push
cd ..

cd merge_client
git pull
# commit the merge
git log
cd ..

cd rebase_client
git fetch
git rebase
git log
cd ..

WAIT A SECOND!  Why did you have to merge on merely adding a new file?!?!?

Let's get a better idea of what is happening.

cd rebase_client
git log --pretty=raw
cd ..

Notice the nice clean chain where each parent is prior commit?  Compare that to merge_client

cd merge_client
git log --pretty=raw
cd ..

client1-README2 has a parent of client1.  In merge_client that is not the prior patch so you have forked the tree and then have to merge back.  Even though no single file is in conflict the tree as a whole has forked.

So, always rebase before pulling.