Search code examples
gitgithubrebasemerge-conflict-resolutiongit-merge-conflict

How do I explicitly mention which branch changes to apply while merging two conflicting branches in git?


I have two branches in git. Master and feature.

O---O (master)
 \ 
  O (feature)

I had a text file which contains Master when it is at master branch head.
The same file contains feature when it is at feature branch head.

I want to rebase the feature branch on to master. Since they both have conflicting changes there will be a rebase conflict (or merge conflict).

I want to know if there is a way to explicitly tell git, I want changes from feature branch in feature branch during the rebase.

This is what I tried:

Image showing setup

Image showing error

We can clearly see that the feature branch text is being overwriten by the master branch text.

Thanks in advance :)


Solution

  • If you want to rebase the feature branch onto master branch, with the changes on feature branch to override master branch in case of any merge conflicts, you must pass --strategy-option with theirs instead of ours.

    When you are checked out on the feature branch, and want to rebase master onto feature, git rebase works by replaying each commit from the feature branch on top of the master branch. Because of this, when a merge conflict happens, the side reported as ours is the master branch, and theirs is the working branch, ie the feature branch.

    From the documentation of git rebase :

    Rebase merge works by replaying each commit from the working branch on top of the upstream branch. Because of this, when a merge conflict happens, the side reported as ours is the so-far rebased series, starting with upstream, and theirs is the working branch. In other words, the sides are swapped.

    So try with git rebase master --strategy-option=theirs and that would make sure that the feature branch changes are considered in case of the merge conflict.