Keep either file in merge conflicts with git

Posted by Bhushan Ahire | Posted in git, Rails | Posted on 26-02-2009

0

So, the scenario is: 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.

There’s two unmerged files here. According to the git checkout manpage, there’s a --theirs and --ours options on the command. The former will keep the version of the file that you merged in, and the other will keep the original one we 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

Sadly, these options are only in Git versions 1.6.1 and up. If you have an older version and don’t feel like upgrading, there’s ways to get around this. To emulate --theirs, we’d 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

Of course, once you’ve got the conflicts worked out, git add whatever changes need to be added in, and git commit away. If you’ve run into other problems with merging that could possibly help out others, comment away!



referred from http://gitready.com/advanced/2009/02/25/keep-either-file-in-merge-conflicts.html

Write a comment

You must be logged in to post a comment.