0

A merge conflict occurred during a rebase, and Git is now asking me to resolve the patch. In cases where both changes are acceptable, how do I tell Git to accept both changes without having to modify the file myself?

E.g. when both changes are acceptable:

<<<<<<< HEAD
const foo = require('./foo');
=======
const bar = require('./bar');
>>>>>>> some commit message

git: can I accept both changes in a merge conflict without editing files? suggests a way to do this for a very specific case without using Git, but I'm looking to do this with native Git commands. A comment mentions doing a union merge, but I couldn't find docs around how to do this in this scenario.

1
  • 1
    Even if Git provides such a command you still need to visually inspect each conflict and decide how to combine the changes. Maybe some of them need yours before theirs while others need their before yours. Both ways look valid for the posted conflict but most of the times it's not that simple.
    – axiac
    Commented Nov 16, 2017 at 16:11

1 Answer 1

1

To do a union merge, you must first extract all three inputs:

git show :1:$path > base
git show :2:$path > ours
git show :3:$path > theirs

(these file names are purely for illustration—in practice you would need to generate a unique temporary file name for most of these file), then run git merge-file:

git merge-file --union ours base theirs

The merged result is now in the file named ours; base and theirs may be discarded.

(Git probably should include a script that does the three extractions, runs git merge-file with any of the ours, theirs, or union options, and puts the result into the work-tree for inspection. It doesn't, but I think it should.)

You must be very careful with union-merge, as it always thinks it succeeded, even if it made a mess of the result.

1
  • I would add that this :N:path/to/a/file notation is explained in the gitrevisions(7) manual page — @neverendingqs, run git help revisions to read it.
    – kostix
    Commented Nov 17, 2017 at 7:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.