60

When I merge a branch in Git to master I often get merge conflicts. Is there a way to merge a branch and just overwrite the stuff in the current branch?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Hunt
  • 4,265
  • 8
  • 45
  • 59
  • You mean like in SVN - use theirs? – stdcall Jan 11 '13 at 10:14
  • My experience with automatically choosing one side for a merge has never been good .. also, isn't it the point of merge conflicts to check what other people changed near the same lines as you before removing their changes? – KristofMols Jan 11 '13 at 13:15
  • 4
    It's me that made the branch locally in the first place.. it's just quicker than trawling through the file and removing the conflicts – John Hunt Jan 11 '13 at 16:42

3 Answers3

79

Add -X ours argument to your git merge command.

Say you are working in your local branch. Then you want to merge in what went in the master:

git merge -X ours master

On the other hand if you are in master and want to merge your local branch into master then @elhadi rightly says you should use theirs:

git merge -X theirs somebranch
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
8

To overwrite your stuff in your branch and take their work, you should make

git merge -X theirs {remote/branch} --> example:origin/master
PhD
  • 11,202
  • 14
  • 64
  • 112
elhadi dp ıpɐɥןǝ
  • 4,763
  • 2
  • 30
  • 34
0

I have found merge doesn't really make the target branch a mirror copy of the source branch.

If that is what you are after, in order to get the branches 100% in sync I have used this procedure:

git merge -X theirs somebranch
git reset somebranch --hard

This will reset the state of the current branch to the HEAD of somebranch post merge. In some cases, you might also want to cleanup your working directory if it is dirty with uncommitted files, the whole procedure would then look like this:

git merge -X theirs somebranch
git reset somebranch --hard
git clean -f
Shanerk
  • 5,175
  • 2
  • 40
  • 36