Ever try to commit and push your changes to master branch and see this annoying message stated that your branch and ‘origin/master’ branch have diverged,

branch diverged

You are in luck, in this tutorial, we are going to talk about how this happen, how to fix it and how to avoid this from happening in the future. Before we talk about how to fix it, we should talk about how this can happen to you. I have drawn the diagram to show you what happened, here is the picture,

branch diverged diagram

As you can see, up until the changes B, your branch and ‘origin/master’ is exactly the same. Starting from changes B, your branch and ‘origin/master’ started to diverged. Your branch started with the changes of E, F and G while the ‘origin/master’ follows the changes of C and D which completely explains the message where it stated “Your branch and ‘origin/master’ have diverged, and have 3 and 2 different commits each, respectively.” Here, the 3 represent the E, F, and G while the 2 represent the C and D.

Note: This typically happens when you are trying to merge your changes to ‘origin/master’ and someone else in your team pushed some other changes after you sync with ‘origin/master’ and before you merge your changes.

Now that we understand the “what”, let’s talk about the “how” here.

There are two ways to fix this problem, one is rebase, another one is merge. They have their advantages and disadvantages and it really depends on your preferences.

git rebase origin/master

Let’s explain it in diagram, when one do rebase, this is what will happen,

rebase branch

Your branch used to be based off changes B, it is now based off changes D. Now, you should have no problem merging your code from your branch to ‘origin/master’.

Advantages: More clean/linear git history that would help people to find what caused the regression quicker and easier if any.
Disadvantages: Merge conflict may become more frequent with possibilities of losing your commit history if done wrong.

git merge origin/master

When one do merge, this is what will happen,

merge branch

As you can see, although your branch and ‘origin/master’ have diverged, it eventually merged together at the end.

Advantages: Easier and quicker to resolve conflicts.
Disadvantages: Non-linear git history.

Now, to fix it, simply pick the one you think that makes more sense in your case and execute the command and follow the instruction given by the git.

I personally like rebase more since it gives more linear git history which would be really helpful when there is a need to look at the git history in the future. However, it might be a pain to resolve conflict while rebasing.

Rule of thumb: Frequently rebase your feature branch to make process of resolving conflict easier in the future.

Tada! You should have now understand why the diverge would happen, the differences between rebase and merge, how to fix it, and how to make your life easier in the future.

Wrapping Up

Hopefully this article will help you to resolve diverged branches and thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started