How do I resolve a git merge conflict in favor of pulled changes?
Basically I need to remove all conflicting changes from a working tree without having to go through all of the conflicts with a git mergetool
while keeping all conflict-free changes. Preferably doing this while pulling, not afterwards.
You can use the recursive "theirs" strategy option:
git merge --strategy-option theirs
From the man:
Note: as the man page says, the "ours" merge strategy option is very different from the "ours" merge strategy.
To resolve all conflicts with the version in a particular branch:
So, if you are already in the merging state, and you want to keep the master version of the conflicting files:
A situation I've found to be useful if I want master to reflect the changes of a new topic branch. I've noticed that -Xtheirs doesn't merge without conflicts in some circumstances... e.g.
In this case the solution I found was
from topicFoo, first merge in master using the -s ours strategy, this will create the fake commit that is just the state of topicFoo. $ git merge -s ours master
check the created merge commit
now checkout the master branch
merge the topic branch back but this time use the -Xtheirs recursive strategy, this will now present you with a master branch with the state of topicFoo.
VS Code (integrated Git) IDE Users:
If you want to accept all the incoming changes in the conflict file then do the following steps.
Similarly you can do for other options like Accept All Both, Accept All Current etc.,
The
git pull -X theirs
answers may create an ugly merge commit, or issue anIf you want to simply ignore any local modifications to files from the repo, for example on a client that should always be a mirror of an origin, run this (replace
master
with the branch you want):How does it work?
git fetch
doesgit pull
but without merge. Thengit reset --hard
makes your working tree match the last commit. All of your local changes to files in the repo will be discarded, but new local files will be left alone.If you're already in conflicted state, and you want to just accept all of theirs:
If you want to do the opposite:
This is pretty drastic, so make sure you really want to wipe everything out like this before doing it.