Force Git to always choose the newer version during a merge?
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.
Related videos on Youtube
bartek
Updated on August 09, 2021Comments
-
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?
-
Rawkode over 11 yearspossible duplicate of git pull from remote.. can I force it to overwrite rather than report conflicts?
-
bartek over 11 yearsbut I do want to do a merge (not override commits), but additionally to resolve conflicts automatically.
-
-
Haywire over 10 years
ours
-theirs
!! Just say it and you understand what the command does! I love Git! :D -
John Dvorak about 10 yearsnote: if you have already used
git merge branch
, you'll need togit merge --abort
before you can do this. -
mcv about 10 yearsDoesn'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 about 10 yearsIf they are untracked, you need to remove (or
git add
) them first. Read a bit aboutgit clean
, it may help you with that. -
zx1986 over 9 yearsAfter ran
git merge ours
, if there are some conflict files, will be any log ? and could I track the ofgit merge ours
? -
Juan Mendez over 8 yearsThis 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 over 2 yearsDO 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.