E---F---G feature
A---B---C master
It’s time to merge your branch to master. You’ll run git checkout master
followed by git merge feature
. The git merge feature
will try to replay changes made on the feature
branch, since it diverged from master (i.e “A”) until its current commit (i.e “G”). But you’ll be met with some merge conflicts. This is because the feature and testing branch both have modified the same file. And now Git doesn't know what changes to keep and what to discard. It handles the control back to you instead of overriding things on its own, which might not be desirable.
You can use git merge --abort
command to abort the merge process when a merge conflict has already occurred.
More on GitHow to Fix the GitHub Error ‘Remote Host Identification Has Changed’
What Is Git Merge Theirs and Ours?
In situations where you want to override changes from one branch to another, you can use two merge strategy options: -Xtheirs
and -Xours
.
If you want to override the changes in the master
branch with your feature branch, you can run the following command after checking out to master:
git merge -Xtheirs feature
And to keep the master branch changes, you can use:
git merge -Xours feature
Interestingly, it works in reverse order if you want to do rebasing of your branch onto the master and keep your changes over the master.
So, if you are on your feature branch, the command you need to run will be:
git rebase master -Xtheirs
And to keep master branch changes over yours, you need to do:
git rebase master -Xours
A tutorial on solving Git merge conflicts. | Video: JetBrains
More on Software EngineeringJava Switch Case Explained
Why the Order is Reversed in Rebasing
If you wish to understand the reasoning behind why the order was reversed, you need to first understand how rebasing works.
Let’s assume we have the following history with the feature branch checked out:
E---F---G feature
A---B---C master
When you rebase onto master via git rebase master
, the process goes through the following steps:
Roll back to the common ancestor commit of the feature and the master branch (i.e “A”).
Save all changes made by commits in the feature branch but that aren’t in the master to a temporary area.
Reset feature branch to current commit on master.
The intermediate history looks like this now:
E---F---G (saved in a temporary area)
feature
A---B---C master
After the reset, all the changes in the temporarily saved area (i.e “E”, “F” and “G”), are applied in turn onto the master branch.
The final new history looks like this:
E---F---G feature
A---B---C master
The key point to remember is that the feature branch was reset to the current master, and changes were applied from there onwards. So, theirs
refers to the feature
branch but not master
and ours
will be the master branch as internally we're on this master branch already.
The following table would make it easier to understand and remember -Xtheirs
and -Xours
strategies.
A table showing when to use Git merge -Xtheirs and -Xours. | Image: Amandeep Singh
When do you use Git merge “theirs”?
Git merge -Xtheirs
is used to override changes in the master branch with changes in the feature branch. The syntax is: git merge -Xtheirs feature
When do you use Git merge “ours”
Git merge -Xours
is used to override changes in the feature branch with those from the master branch. The syntax for -Xours
is: git merge -Xours feature