While doing a big git merge and having tough conflicts to resolve recently it hit me there is an easier way to do that. I’ll explain in this post.
So the situation is: I’m doing a feature in a separate branch, and need to merge in latest changes from
master. When I try to merge it, I’ve got a few big conflicts, and one file is marked conflicted entirely (due to the difference of EOL characters). It’s a pain in the ass to resolve the conflicts by hand.
1. Enable three-way conflict style
This is not necessary, but will help with identifying the differences in conflict (see
man git-config or http://git-scm.com/docs/git-config):
2. Enable rerere
This is not necessary either, but will help you re-resolving conflicts faster:
It records the resolutions of the conflicts you made once, and then reapplies them if the conflict is the same. Neat feature!
The idea is to rebase your branch, fixing the conflicts on the way, save the result, then go back, do the merge, and just copy the result over!
Let’s assume, you’re working on the
awesomeFeature branch, and need to merge in the
master branch. Here’s a quick and dirty sequence of commands:
Make a copy of the branch and switch to it:
git checkout -b awesomeFeature_temp.
Rebase it against
git rebase master. You’ll have your conflicts, but they will be in smaller batches, according to the commits you’ve done in your branch. It’s way easier to fix them this way, looking at the original commits. After fixing each conflict commit, run
git rebase --continue.
Now you’ve got the result, equivalent to that of the merge. Switch to the original branch:
git checkout awesomeFeature, and merge the
git merge master. Most likely, all of your conflicts are already resolved (thanks to
rerere!). If not, you should get the files from the result:
git checkout awesomeFeature_temp -- file. Commit, you’re done!
Remove the copy branch:
git branch -D awesomeFeature_temp.
If you’ve used the method, please let me know if you had any issues with it. Thank you!