Sometimes when resolving a merge conflict, you may want to keep one file instead of the other. You don’t need to open up the files and fix potentially hundreds of conflicts; you just want to choose the one you want and be done with it. Unfortunately, this wasn’t exactly straightforward in older versions of Git, but more recent versions have made it easier. Big thanks to Kevin Old for his post on the subject, which reminded me about this issue.
The Scenario
You’re in the middle of a merge, and you want to keep one file or the other.
$ git merge master
Auto-merged _layouts/default.html
CONFLICT (content): Merge conflict in _layouts/default.html
Auto-merged index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
Here, we have two unmerged files. According to the git checkout manpage, there are –theirs and –ours options. The former will keep the version of the file that you merged in, and the latter will keep the original one you had.
The following commands will keep the original file for index.html and then use the merged-in file only for _layouts/default.html.
git checkout --ours index.html
git checkout --theirs _layouts/default.html
Workarounds for Older Git Versions
Sadly, these options are only available in Git versions 1.6.1 and up. If you have an older version and don’t feel like upgrading, there are ways to get around this. To emulate –theirs, you can do:
git reset -- _layouts/default.html
git checkout MERGE_HEAD -- _layouts/default.html
And for –ours:
git reset -- index.html
git checkout ORIG_HEAD -- index.html
Once you’ve resolved the conflicts, remember to git add the necessary changes and then git commit them. If you’ve encountered other merging issues that could help others, feel free to share in the comments!