Git Tip: Merge Branches Taking Always Changes From One of Them

, git, tips

This is a trick I discovered a few days ago and made me cry remembering the time I lost fixing manually conflics when I already knew that the changes I wanted were all from same branch.

Never again using merge strategies.

1
git merge [-s recursive] -X[theirs|ours]

If you, being in the branch master, run git merge -Xtheirs experimental_feature the conflics will be automatically resolved taking the changes from the experimental_feature branch. The opposite, git merge -Xours experimental_feature, will keep the code from the current branch.

The -s recursive forces the merging strategy to the recursive mode, which is the only strategy that accepts the theirs and ours options. I did not know there where different strategies for merge branches, but there are several. This is the default strategy when pulling or mergin one branch into another, so most of the time won’t have to specify this but is good to know. (did you even know that you can merge more than two branches at once?)

Hope it helps.

Happy merging.

Comments: