Rebase – Keeping History Clean in Git

Arjit Sharma
01 Min

Its the key to a Clean and Linear History.

Instead of merging (which creates an extra merge commit), rebasing applies your commits one by one on top of the new base.

Generally when we have a feature branch and we merge it with our master, but it has few drawbacks like

  • history gets more complicated
  • There is an extra change when 2 branches are merged

image.png

What we really want our history to look is like this

image.png

Thats where rebase comes in.

git rebase <base-branch>
# If conflict occurs then ->
git rebase --continue

Rebase in Action

You have a feature1 branch based on an old master branch. Before merging, you want to update it to the latest master changes.

git checkout feature1
git rebase master  # Rebase feature1 onto the latest master

# After resolving the conflict
git add .
git rebase --continue

After our rebase this is how our codebase looks -

image.png

Now changes are in one line and we can merge feature1 branch into master branch ( as master is still behind )

git checkout master
git merge feature1
git log --all --graph --oneline

Note - You can see Fast-Forward in terminal, that means there arent any new changes that need to be made so master branch pointer moves to HEAD.