How to rebase your branch on master

Before starting to explain how to rebase your branch on another branch (master for us), let us first understand what is a rebase and why we should use it.

Imagine you are working on a branch called my-branch, and you pushed some commits. However, the master branch is evolving at the same time and contains changes that you want to pull to stay up to date on your branch. Your git tree will look like this :

          A---B---C my-branch
    D---E---F---G---H master

And what you want to have is this git tree where your branch moved on top of the latest changes of master.

                      A'---B'---C' my-branch
    D---E---F---G---H master

This is possible using git rebase. I will guide you through the steps. You will see it is very simple.

Initiate the rebase

First, you need to be sure you have the latest version of master on your machine. So you start to checkout master and pull it.

git checkout master
git pull

Now that your master is up-to-date, you can go back to your branch and rebase on master.

git checkout my-branch
git rebase master

Now the result depends on the modifications that happened on the master branch. Either everything will go well, and the rebase will succeed from the first try. Or you will have some conflicts, and you will need to resolve them before being able to push everything.

Best case scenario:

The rebase has no conflicts, you can push force and your rebase is done.

push -f

Worst case scenario:

You have some conflicts. Take the time to resolve the conflicts in your IDE. Once it’s done, you can continue to rebase and push.

git rebase --continue
git push -f

Here is how to rebase your branch on master in a few easy steps. Hope it was useful.