Programster
Programster

Reputation: 12764

Configure Git to create multiple files for merge conflicts

I just tried to merging one of my branches, but unfortunately, there was a merge conflict because both my branch and the master added a file with the same name. Hence after sending the merge command I get:

...
Auto-merging Defines.php
CONFLICT (add/add): Merge conflict in Defines.php
Automatic merge failed; fix conflicts and then commit the result.

My problem is that git will alter the file to create a unified diff, rather than creating separate files to work from which I am used to with SVN. E.g. I would get three new versions of the file with the extensions .working, .merge-right and .merge-left.

Question

Is it possible to configure Git to create multiple files instead of creating a unified diff, when it encounters a merge conflict? This way I can use a tool like meld to view a side-by-side diff. Alternatively, is it possible to manually do this with commands similar to git checkout --ours [filename] and git checkout --theirs [filename] but have it write to a new file rather then the original?

Upvotes: 2

Views: 478

Answers (2)

max630
max630

Reputation: 9238

I don't know how to make it automatically at conflict but you can extract them from index manually:

git cat-file -p :1:dir/file >dir/file.base
git cat-file -p :2:dir/file >dir/file.local
git cat-file -p :3:dir/file >dir/file.remote

See gitrevisions

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45649

I have sometimes done this manually. As you've noted, this is kind of a pain in the neck because it's not so simple to check a file out to an alternate path. You could of course check out the base version, then rename it, then check out "their" version, then rename it, then check out "our" version, then rename it. And since this would be scripted, the extra steps aren't that bad. Alternately you could cobble something together from plumbing commands, though I'd have to think that one through and it doesn't seem worth it when the above works fine.

If you want to configure git to do this on its own, you probably need to write a custom merge driver. Merge drivers have access to the three versions of the file so putting them at appropriate paths in the working tree would be easy. See "Defining a custom merge driver" in the gitattributes documentation (https://git-scm.com/docs/gitattributes). Apart from having to work out the details of delegating to the regular merge strategies in most respects, the down side is that once this has run you'd have the .working, .merge-left, and .merge-right files to be cleaned up from your work tree. (And of course it shouldn't be an issue, but there's the potential for conflicting with the name of an actual file.)

Upvotes: 2

Related Questions