W
Wycliffe A. Onyango
Guest
Git Rebase: What It Is, Why Itβs Useful, and How to Do It
When collaborating on a project, developers often create branches to work on specific features while the main branch (
master
or main
) continues to receive updates from other teammates. Eventually, the feature branch needs to be synchronized with the latest changes in the main branch.There are two ways to achieve this: merging and rebasing.
What is Rebase?
Rebase is a way of updating your feature branch with the latest changes from the main branch, but without creating an extra merge commit. Instead, Git takes your feature commits and βreplaysβ them on top of the current main branch.
Visualization:
Before rebase:
Code:
master: A -- B -- C
feature: \-- D -- E
After rebase:
Code:
master: A -- B -- C
feature: \-- D' -- E'
Here,
D
and E
are your feature commits. After rebasing, they appear after C
, as if your work had started from the newest version of master
.Why Use Rebase?
- Cleaner history β avoids unnecessary merge commits, making the project timeline easier to read.
- Easier debugging β linear history helps when using tools like
git log
orgit bisect
. - Keeps commits grouped β your feature work is neatly stacked on top of the latest main branch.
- Better collaboration β rebased branches are often simpler to review in code reviews.

Rebasing Exercise
Hereβs how I did rebase on the repository:
/usr/src/kodekloudrepos/beta.git
:Go to the repository:
Code:
cd /usr/src/kodekloudrepos/beta.git
Fetch the latest updates from the remote:
Code:
git fetch origin
Switch to the feature branch:
Code:
git checkout feature
Rebase the feature branch with master:
Code:
git rebase origin/master
- If conflicts appear, fix them manually.
After fixing, run:
Code:git add <file> git rebase --continue
Push the updated feature branch back to the remote:
Code:
git push origin feature --force
Final Thoughts
By rebasing, the feature branch was updated to include the latest changes from the master branch, without creating a merge commit. This keeps the history clean and focused on the actual work done in the feature.
Continue reading...