114

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?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
bartek
  • 2,921
  • 5
  • 26
  • 30
  • possible duplicate of [git pull from remote.. can I force it to overwrite rather than report conflicts?](http://stackoverflow.com/questions/4785107/git-pull-from-remote-can-i-force-it-to-overwrite-rather-than-report-conflicts) – Rawkode Nov 27 '12 at 22:32
  • but I do want to do a merge (not override commits), but additionally to resolve conflicts automatically. – bartek Nov 27 '12 at 22:35

3 Answers3

209

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".

Renato Zannon
  • 28,805
  • 6
  • 38
  • 42
  • 10
    `ours`- `theirs`!! Just say it and you understand what the command does! I love Git! :D – Haywire Oct 26 '13 at 13:22
  • 17
    note: if you have already used `git merge branch`, you'll need to `git merge --abort` before you can do this. – John Dvorak Apr 12 '14 at 20:21
  • 1
    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. – mcv May 02 '14 at 15:12
  • 2
    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. – Renato Zannon May 02 '14 at 20:01
  • After ran `git merge ours`, if there are some conflict files, will be any log ? and could I track the of `git merge ours` ? – zx1986 Aug 28 '14 at 09:56
23

I use this,

git fetch --prune
git reset --hard origin/master
wolfgang
  • 7,281
  • 12
  • 44
  • 72
  • 3
    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) – Juan Mendez Nov 28 '15 at 04:12
  • 2
    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. – Marcelino Lucero III Dec 08 '21 at 05:03
1

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.

SmoothKen
  • 131
  • 1
  • 2