Resolve Git merge conflicts in favor of their changes during a pull
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.
git pull -s recursive -X theirs <remoterepo or other repo>
Or, simply, for the default repository:
git pull -X theirs
If you're already in conflicted state...
git checkout --theirs path/to/file
You can use the recursive "theirs" strategy option:
git merge --strategy-option theirs
From the man:
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.
This should not be confused with the ours merge strategy, which does
not even look at what the other tree contains at all. It discards
everything the other tree did, declaring our history contains all that
happened in it.
theirs
This is opposite of ours.
Note: as the man page says, the "ours" merge strategy-option is very different from the "ours" merge strategy.
If you're already in conflicted state, and you want to just accept all of theirs:
git checkout --theirs .
git add .
If you want to do the opposite:
git checkout --ours .
git add .
This is pretty drastic, so make sure you really want to wipe everything out like this before doing it.
OK so, picture the scenario I was just in:
You attempt a merge
, or maybe a cherry-pick
, and you're stopped with
$ git cherry-pick 1023e24
error: could not apply 1023e24... [Commit Message]
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Now, you view the conflicted file and you really don't want to keep your changes. In my case above, the file was conflicted on just a newline my IDE had auto-added. To undo your changes and accept their's, the easiest way is:
git checkout --theirs path/to/the/conflicted_file.php
git add path/to/the/conflicted_file.php
The converse of this (to overwrite the incoming version with your version) is
git checkout --ours path/to/the/conflicted_file.php
git add path/to/the/conflicted_file.php
Surprisingly, I couldn't find this answer very easily on the Net.
The git pull -X theirs
answers may create an ugly merge commit, or issue an
error: Your local changes to the following files would be overwritten by merge:
If 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):
git fetch && git reset --hard origin/master
How does it work? git fetch
does git pull
but without merge. Then git 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.