Resolve Git merge conflicts in favor of their changes during a pull

1,149,431

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
1,149,431
sanmai
Author by

sanmai

Updated on December 30, 2021

Comments

  • sanmai
    sanmai over 2 years

    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.