Search This Blog
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.
Labels:
git
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment