Is there a "theirs" version of "git merge -s ours"? Is there a "theirs" version of "git merge -s ours"? git git

Is there a "theirs" version of "git merge -s ours"?


Add the -X option to theirs. For example:

git checkout branchAgit merge -X theirs branchB

Everything will merge in the desired way.

The only thing I've seen cause problems is if files were deleted from branchB. They show up as conflicts if something other than git did the removal.

The fix is easy. Just run git rm with the name of any files that were deleted:

git rm {DELETED-FILE-NAME}

After that, the -X theirs should work as expected.

Of course, doing the actual removal with the git rm command will prevent the conflict from happening in the first place.


Note: A longer form option also exists.

To use it, replace:

-X theirs

with:

--strategy-option=theirs


A possible and tested solution for merging branchB into our checked-out branchA:

# in case branchA is not our current branchgit checkout branchA# make merge commit but without conflicts!!# the contents of 'ours' will be discarded latergit merge -s ours branchB    # make temporary branch to merged commitgit branch branchTEMP         # get contents of working tree and index to the one of branchBgit reset --hard branchB# reset to our merged commit but # keep contents of working tree and indexgit reset --soft branchTEMP# change the contents of the merged commit# with the contents of branchBgit commit --amend# get rid off our temporary branchgit branch -D branchTEMP# verify that the merge commit contains only contents of branchBgit diff HEAD branchB

To automate it you can wrap it into a script using branchA and branchB as arguments.

This solution preserves the first and second parent of the merge commit, just as you would expect of git merge -s theirs branchB.


Older versions of git allowed you to use the "theirs" merge strategy:

git pull --strategy=theirs remote_branch

But this has since been removed, as explained in this message by Junio Hamano (the Git maintainer). As noted in the link, instead you would do this:

git fetch origingit reset --hard origin

Beware, though, that this is different than an actual merge. Your solution is probably the option you're really looking for.