Last Updated: February 25, 2016
·
18.16K
· daraff

git merge strategy if you have a conflict

Sometimes if you want to merge a branch into another and you are sure, that you want to take one branch, if there is a conflict, you can set the merging strategy.

If you're in the branch master and want to merge back a branch from a release branch and want to take the release file, when a conflict happens, you can take the theirs strategy

git merge --strategy-option theirs release-7.15.1 -m "merge release-7.15.1 to master"

If you're in the branch master and want to merge back a branch from a release branch and want to take the master file, when a conflict happens, you can take the ours strategy

git merge --strategy-option ours release-7.15.1 -m "merge release-7.15.1 to master"

2 Responses
Add your response

From experience, this is a very dangerous thing to do -- using the 'ours' merge strategy should be reserved for extraordinary situations.

Imagine you have a hotfix that has been merged into production... but has not been merged back into your development branch (lets call it master) yet. If you merged with the command above, git log would show that the hotfix commit had been merged into master, but the actual change would be silently discarded. (That is, the history is joined, but the content is lost.)

Much safer would be to handle the conflict in the regular way:

git merge release-7.15.1 -m "merge release-7.15.1 to master"

...and ensure that the only conflict that arises is the 'release' file. When that happens, do a checkout:

git checkout --ours ./release

At this point, the release file in the working directory will be the same as the file recorded in the tip of master (that is, HEAD). You can now add the file to the index and commit -- assuming that no other conflicts arise.

This way, you achieve what you want -- the release file staying the way it was in master w/o having to repeatedly dig into it to resolve things-- without playing Time Lord too much with history. That's the problem with 'ours' as a merge strategy: it messes with people's expectations of how a branch's history works in unexpected and surprising ways.

over 1 year ago ·

Hey @RsrchBoy, thank you very much for your feedback. I was not aware of this problem, because until now i almost always used the theirs strategy.

over 1 year ago ·