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.

neverendingqs
  • 4,006
  • 3
  • 29
  • 57
  • 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 Nov 16 '17 at 16:11

1 Answers1

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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • I would add that this `:N:path/to/a/file` notation is explained in the [`gitrevisions(7)` manual page](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html) — @neverendingqs, run `git help revisions` to read it. – kostix Nov 17 '17 at 07:09