Advanced Git Concepts : Rebasing

Sushan Shakya
3 min readJun 29, 2023

--

Article assumes user has some familiarity with git

Consider that we have 2 developers working on 2 different features,
the Git tree in this case will look as follows :

Here,
These 2 developers are working parallelly on 2 different features.
We can think of feature1 and feature2 as being 2 parallel worlds or different timelines of the future.

In fact, the dev branch doesn’t have any idea about feature1 or feature2 .

Now,
Let’s say that both feature1 and feature2 are completed at the same time.

In such case,
Developer of feature1 will create a pull request from feature1 to dev .
Developer of feature2 will create a pull request from feature2 to dev .

This point is time is the decisive moment.

dev is our main timeline, we can pick to merge feature1 or feature2 .

Picking feature1

Let’s say we pick to merge feature1 first then our main timeline will be that, our project state was in dev then feature1 was added. The git tree will look like :

Here,
dev now knows that feature1 has happend.
It doesn’t know anything about feature2 .

feature2 knows about old dev but doesn’t know anything about feature1 being part of dev .

If we want the feature2 branch to know about changes and addition of feature1 to dev then we can rebase feature2 .

Rebasing

The idea of rebasing is simple.
We’ll break the feature2 branch from it’s origination point and then attach it somewhere else. Let’s look at the tree to understand this :

To break it off it’s base and attach it to the new dev commits, we can use following command :

(feature2) ~ git rebase dev

(feature2) indicates that we are currently in feature2 branch

This command, then breaks off the base of feature2 and attaches it to the dev . We can visualize it as :

Now,
feature2 knows about the existence of feature1 which is merged into dev .

This is the one of the powers of rebasing.

When rebasing we might have merge conflicts if feature1 and feature2 branch have changes to the same file. In such case, we resolve the merge conflicts, then add the resolved files as :

(feature2) (rebasing) ~ git add .

Then, we continue with rebasing as :

(feature2) (rebasing) ~ git rebase — continue

If we want to cancel the rebase, we use :

(feature2) (rebasing) ~ git rebase — abort

We can do the similar thing if we wanted to merge feature2 before feature1 .

--

--