Force Git to always choose the newer version during a merge?

92,893

Solution 1

It is not exactly the "newer" version, but you can tell git to always prefer the version on the current branch using git merge branch -X ours, or to prefer the version of the branch being merged, using git merge branch -X theirs.

From man git-merge:

ours:

This option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree that do not conflict with our side are reflected to the merge result. For a binary file, the entire contents are taken from our side.

theirs:

This is the opposite of "ours".

Solution 2

I use this,

git fetch --prune
git reset --hard origin/master

Solution 3

Take a look at my answer in Git timestamp based automated sync

Essentially we have to do manual timestamp comparison. I do not think git merge has any built-in utility for this.

Share:
92,893

Related videos on Youtube

bartek
Author by

bartek

Updated on August 09, 2021

Comments

  • bartek
    bartek almost 3 years

    Let's assume I merge git and there is a merge conflict.

    My question is: how can I force git to always choose the newer version of code in conflict so I won't need to resolve the conflict by hand?

  • Haywire
    Haywire over 10 years
    ours- theirs!! Just say it and you understand what the command does! I love Git! :D
  • John Dvorak
    John Dvorak about 10 years
    note: if you have already used git merge branch, you'll need to git merge --abort before you can do this.
  • mcv
    mcv about 10 years
    Doesn't work for me. It still aborts the merge. error: The following untracked working tree files would be overwritten by merge: I don't even know why these files are in this branch in the first place, but they should be overwritten, and git refuses.
  • Renato Zannon
    Renato Zannon about 10 years
    If they are untracked, you need to remove (or git add) them first. Read a bit about git clean, it may help you with that.
  • zx1986
    zx1986 over 9 years
    After ran git merge ours, if there are some conflict files, will be any log ? and could I track the of git merge ours ?
  • Juan Mendez
    Juan Mendez over 8 years
    This solution helped me to fix unmerged conflicts when all I wanted was to override with master branch. i used git reset --hard master (from local)
  • Marcelino Lucero III
    Marcelino Lucero III over 2 years
    DO NOT USE THIS unless you know exactly what it does, it is a good way for beginners to loose a lot of work. This will not merge the code at all, let alone choose the most recent code. It simply overwrites every thing in your branch with what is in the remote repo. It is a very useful cmd (I use it all the time after tinkering with stuff to try out different things) but make sure there is no new code in your local branch that you want to save before you use it.