40

Some time ago I created a branch from my master branch. Let's call it new_feature. While I was working on new_feature, the master branch followed its normal evolution. Now that new_feature is ready to be merged into master I see some conflicts, all of them are into files that are totally unrelated to the actual new feature (like some config files and the likes that have changed on master). I'm going to solve the conflicts manually but I was wondering, since it's a situation that happens quite often, how I could just merge the new_feature branch into master while always keeping the master version of the files in case of conflict?

I'm sure it's easy and is related to something like 'keep version' but since it's a pretty sensitive subject I'd rather ask than be sorry.

Patrizio Bertoni
  • 2,582
  • 31
  • 43
Bastian
  • 5,625
  • 10
  • 44
  • 68
  • 1
    The documentation describes several merge strategies, including the one you're interested in, in detail. You should always be able to recover and try again if anything goes wrong, so a bit of experimentation wont hurt, either. – Carl Norum Jul 17 '13 at 15:37
  • Check this SO question out: http://stackoverflow.com/questions/528659/git-automatic-conflict-resolution – Ricardo Sanchez-Saez Jul 17 '13 at 15:46
  • Thanks @CarlNorum , are you referring to the recursive strategy with option ours? I'm not sure to understand if this strategy keeps the whole tree as master. – Bastian Jul 17 '13 at 15:46
  • This isn't super relevant to the OP's question, but if you're asking this same question because you reverted a merge and now want to re-do the merge, check out https://stackoverflow.com/questions/1078146/re-doing-a-reverted-merge-in-git for solution(s) to that situation. I ended up here on my way to dealing with that, but found out simply preferring the current branch will not work well in the case of fixing up a messy merge/unmerge/remerge situation — attempting this does avoid most conflicts at the git level *but* leaves a lot of changes reverted in the resulting code. – natevw Mar 22 '22 at 18:58

1 Answers1

49

As mention in the comment there are several strategies in the documentation. You can also find them here: http://git-scm.com/docs/git-merge

You are looking for either git merge -s recursive -X ours or git merge -s recursive -X theirs depending on the branch that you are on. Be careful with these as you can accidentally miss changes to your files from the other branch and these would be overwritten.

Another method, which I prefer due to more control, is to git checkout <other-branch> -- <list of files>. This way I don't accidentally overwrite a file with the wrong changes.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • 2
    Just to be sure I understand well, in my case if I do `git merge -s recursive -X ours new_feature` from master I will get the changes from new_feature but the conflicting files will stay as they are on master? – Bastian Jul 17 '13 at 15:52
  • `ours` => This option conflicts to be auto-resolved cleanly by favoring the 'our' version. Changes from the 'theirs' side are automatically incorporated if they do not conflict. https://www.atlassian.com/git/tutorials/using-branches/merge-strategy – MJeremy Jan 14 '20 at 09:21