1

I have two Git branches, mybranch (which is checked out) and theirbranch. I would like to merge theirbranch into mybranch, resolving conflicts by keeping my version of a file if I've changed it (i.e. it has changed on mybranch since the last common ancestor) and their version of the file if I haven't.

How can I do this in Git?

Two things that don't work:

  • git merge -s ours: this keeps my copy of all files, even if I haven't changed them.
  • git merge -Xours: this keeps non-conflicting hunks from theirbranch in files that I've changed; I want to ignore all hunks from theirbranch in files I've changed.
Kerrick Staley
  • 1,583
  • 2
  • 20
  • 28

1 Answers1

1

Here is one solution (assumes use of a Bash-like shell):

git merge theirbranch
git diff --name-only "$(git merge-base theirbranch HEAD)"..HEAD \
| xargs git checkout HEAD
git commit

It would be great if someone could suggest something shorter!

Kerrick Staley
  • 1,583
  • 2
  • 20
  • 28
  • That's fine for getting your version of conflicted files (as long as there are no rename issues). An alternate method would be to read the index (`git ls-files --stage`) and note where there are stage 1, 2, and 3 entries for files, or to use `git status --porcelain` and read that, but these would be longer. Note that for scripting purposes you might want to use `git diff-tree` (a plumbing command) rather than `git diff` (porcelain). – torek Aug 01 '20 at 01:39