WTF is Git rebase

WTF is Git rebase

I've never used git rebase at work, but I see many people talking about it, and there's the classic "merge vs rebase" debate. So, here I am, learning about rebase, comparing it with merge, and writing a blog for myself.

What does rebase do

Rebase is a Git utility used when a team is working on a repository and wants to integrate changes from one branch to another. Git merge is another tool for this purpose, and we will explore the differences between them in the next section. First, let's dive into what rebase does.

Let's start with having a main branch and creating a feature branch for developing a new feature. Let's say we made 2 commits in this new feature branch. Meanwhile, our team has made 1 new commit in the main branch. Now we are in a situation where the main branch and the feature branch have diverged. We want to get the changes from the main branch into the feature branch keeping the history clean and making it look like we cut the feature branch from the latest main branch. Rebase can help us do exactly that. When we rebase our feature branch onto the main branch, the base of our feature branch will be updated, making it appear as if we created this feature branch from the latest commit in the main branch. Confused? Look at the image below to understand this better.

Let's create a new git repo and see how this works using git logs.

mkdir rebase_testing
cd rebase_testing
git init

Now I'll make an initial commit in main, then cut a feature branch. Make 2 commits in the feature branch. Note: all are empty commits.

git commit --allow-empty -m "main: commit 1"
git checkout -b feature_branch
git commit --allow-empty -m "feature: commit 1"
git commit --allow-empty -m "feature: commit 2"

Now let's make a new commit in the main branch

git checkout main
git commit --allow-empty -m "main: commit 2"

This is how git logs look in this state in both the branches.


Feature branch:

Now let's rebase feature branch onto main

git checkout feature_branch
git rebase main

Now let's look at the commit history of the feature_branch

As you can see, the base commit of the feature branch has changed from main: commit 1 to main: commit 2, just like in the image. And that's how rebase works. Now we can merge the feature branch into the main branch by running git checkout main and git merge feature_branch.

How is it different from git merge

Git merge is also a Git utility used to integrate changes from one branch to another. However, unlike rebase, merge does not rewrite commits. Instead, it creates a new merge commit. Look at the image below for a better understanding.

A new merge commit is created only when the two branches have diverged, as in the example above. However, if the main branch doesn't have any new commits, Git can directly move the main branch tip to the feature branch tip. This is called a fast-forward merge.

Now it is easy to see that, after rebasing the feature branch onto the main branch, the feature branch is no longer diverged from the main branch. So, merging the feature branch into the main branch will result in a fast-forward merge without a new merge commit.

This is why some developers prefer "rebase and merge" instead of "just merge".

Golden rule of rebasing

The golden rule of rebasing is to never use it on public branches like main. Since rebase can rewrite the commits of a branch, your main branch will then be different from everyone else's main branch, which is not desirable.


If you want a clean commit history without any merge commits, you can choose to rebase and then merge. But if you want to keep all the merge history and avoid the risk of rewriting commit history, go for the classic merge.

The go-to docs when I need to learn something about Git are the Atlassian docs. This blog is also inspired by them. Thanks for reading and happy coding !