tags:

views:

742

answers:

2

I don't want a visual merge tool, and I also don't want to have to vi the conflicted file and manually choose the between HEAD (mine) and the imported change (theirs). Most of the time I either want all of their changes or all of mine. Commonly this is because my change made it upsteam and is coming back to me through a pull, but may be slightly modified in various places.

Is there a command line tool which will get rid of the conflict markers and choose all one way or another based on my choice? Or a set of git commands which I can alias myself to do each one.

# accept mine
alias am="some_sequence;of;commands"
alias at="some_other_sequence;of;commands"

Doing this is rather annoying. For 'accept mine' I have tried:

randy@sabotage ~/linus $ git merge test-branch
Auto-merging Makefile
CONFLICT (content): Merge conflict in Makefile
Automatic merge failed; fix conflicts and then commit the result.

randy@sabotage ~/linus $ git checkout Makefile 
error: path 'Makefile' is unmerged

andy@sabotage ~/linus $ git reset --hard HEAD Makefile 
fatal: Cannot do hard reset with paths.

How am I supposed to get rid of these change markers?

I can do:

git reset HEAD Makefile; rm Makefile; git checkout Makefile

But this seems rather round about, there must be a better way. And at this point, I'm not sure if git even thinks the merge happened, so I don't think this necessarily even works.

Going the other way, doing 'accept theirs' is equally messy. The only way I can figure it out is do:

git show test-branch:Makefile > Makefile; git add Makefile;

This also gives me a messed up commit message, which has Conflicts: Makefile in it twice.

Can someone please point out how to do the above two actions in a simpler way? Thanks

+1  A: 

git mergetool is your answer.
It has been introduced in March 2007 precisely to run an appropriate merge conflict resolution program.

You can configure it to refer to any script.

If that script is a simple : git checkout --their (for instance), you just solved all your merge conflicts by accepting the imported changes.

You can find here an example of git mergetool configuration, although it is for visual diff tools.
In your case, the custom merge tool script should correctly indicates the success of a merge resolution with its exit code, with the configuration variable "mergetool.<tool>.trustExitCode" being set to true.

Another similar issue (in its principle) has been described there. Yet another illustration of that mergetool feature can be found in the aptly named: "git mergetool: when >>>>>>> <<<<<<< just doesn’t cut it".

If you run "git mergetool" without specify a file, it will pick the first one in the list and not let you move on till you’ve merged it completely. Be aware that it considers a file merged if it has been modified and the exit code of the merging program indicates that everything went well and marks the file as merged.

git mergetool takes two optional arguments: -t and a list of filenames.
"-t" lets you specify a mergetool without having to set any config options.

VonC
+8  A: 

The solution is very simple. "git checkout <filename>" tries to check out file from the index, and therefore fails on merge. What you need to do is "git checkout HEAD -- <filename>" (i.e. checkout a commit) or "git checkout --ours -- <filename>" to bring file to your version, or "git checkout test-branch -- <filename>" or "git checkout --theirs -- <filename>".

Or you can use "git show :2:<filename> > <filename>" (stage 2 is ours) or in the opposite way "git show :3:<filename> > <filename>" (stage 3 is theirs), but the you would also need to run "git add <filename>" to mark it as resolved.

Jakub Narębski
Thanks. I'll add that these flags are very new, they aren't in 1.6.0 which is what I was using from source (and distro's like Ubuntu have even older versions of Git)
nosatalian