9

Currently I am working in a feature branch that will soon be merged back into master. I recently tried just that and had some merge conflicts, which I had to fix manually.

So, is it possible to tell git to always use the version from the merged branch in order to avoid merge conflicts beforehand? In this case I fixed the conflicts manually but always chose the version from the merged branch, so this would save me some tedious work.

Max
  • 15,693
  • 14
  • 81
  • 131

2 Answers2

18

You can do exactly this in git with the following command, assuming that feature is the name of your feature branch:

git merge -s recursive -X theirs feature

This says to use the "recursive" merge strategy, but with the "theirs" option. This means that when there is a conflict, it will be automatically resolved by taking the version of the hunk from the feature branch, not your current branch. (Note that this is completely different from the "theirs" merge strategy, which has now been removed from git.)

This feature was introduced in git v1.7.0.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Are you sure? Didn't the strategy make all files come over from One side - not just the ones that conflicted? – Adam Dymitruk May 30 '11 at 05:52
  • Yes, that's what the old `theirs` merge strategy did, but the `theirs` option to the `recursive` strategy does something different - it does a normal merge but automatically resolves any conflicts in favour of "their" version. – Mark Longair May 31 '11 at 11:35
  • good to know! Seems a bit dangerous though. What about the conflicted file itself? Would recursive-theirs use the changes that did not conflict? Or would it grab the entire file? – Adam Dymitruk May 31 '11 at 19:40
  • @adymitruk: the description in the `git merge` man page says it's only used for conflicting hunks, so it would include changes from ours that didn't conflict, even if there were other hunks in the file that did conflict. – Mark Longair Jun 01 '11 at 08:59
  • I guess you're at the mercy of what it considers a hunk for that merge. Feeling a little uneasy about it.. but that's why we have unit tests :) – Adam Dymitruk Jun 01 '11 at 16:33
0

You can't use the recursive "ours" strategy. It would omit your changes that didn't conflict.

You could script getting the file names of the conflicted files, and do a git checkout --ours -- filename followed by a git add filename.

If you're getting the same conflicts over and over, turn on rerere and that may be enough so you don't have to resolve the conflicts.

Hope this helps.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141