Introduction
When working with Git, managing commit history is just as important as writing clean code. One of the most powerful — and sometimes misunderstood — tools for this purpose is git rebase.
What is Git Rebase?
At a high level, git rebase is used to move or “replay” commits from one branch onto another.
Instead of merging branches together (like git merge), rebase rewrites commit history to make it look like your work was built on top of another branch from the beginning.
The Core Idea
Let’s say you have:
main: A --- B --- C
feature: D --- E
Now, new commits are added to main:
main: A --- B --- C --- F --- G
feature: D --- E
If you run:
git rebase main
Git will:
- Temporarily remove commits
DandE - Move your branch to
G - Replay
DandEon top ofG
Result:
main: A --- B --- C --- F --- G
feature: D' --- E'
Note:
D'andE'are new commits (rewritten versions)
Why Use Rebase?
1. Clean History
Rebase creates a linear commit history, making it easier to read and understand.
Instead of this:
/--- D --- E
A --- B --- C --- M
You get:
A --- B --- C --- D --- E
2. Better for Code Reviews
Reviewing a clean, linear history is much easier than dealing with merge commits.
3. Avoids Unnecessary Merge Commits
Rebase prevents clutter like:
Merge branch 'main' into feature
Rebase vs Merge
| Feature | git merge | git rebase |
|---|---|---|
| History | Non-linear | Linear |
| New commits | Adds merge commit | Rewrites commits |
| Safety | Safe (no rewrite) | Risky if misused |
| Collaboration | Preferred | Careful usage needed |
Basic Commands
Rebase current branch onto another
git rebase main
Interactive Rebase
git rebase -i HEAD~3
This allows you to:
- Edit commits
- Squash commits
- Reorder commits
- Remove commits
Example:
pick 123abc Add login
pick 456def Fix bug
pick 789ghi Improve UI
You can change to:
pick 123abc Add login
squash 456def Fix bug
pick 789ghi Improve UI
What Happens Internally?
When you run rebase:
- Git finds the common ancestor
- Extracts your commits as patches
- Resets your branch pointer
- Applies patches one by one
This is why commit hashes change — they are new commits.
Handling Conflicts
During rebase, conflicts may occur.
Git will pause and show:
CONFLICT (content): Merge conflict in file.c
Steps to resolve:
- Fix the conflict manually
- Stage the file:
git add file.c
- Continue:
git rebase --continue
If needed:
git rebase --abort
Golden Rule of Rebase
❗ Never rebase public/shared branches
Why?
Because rebase rewrites history, and this can break other developers’ work.
When Should You Use Rebase?
Use rebase when:
- Working on local feature branches
- Cleaning up commits before pushing
- Keeping history linear
Avoid rebase when:
- Working on shared branches
- The branch is already pushed and used by others
Advanced Example: Squashing Commits
Before:
Add feature
Fix typo
Fix bug
Improve code
After squash:
Add feature (clean version)
Command:
git rebase -i HEAD~4
Common Mistakes
1. Rebasing Shared Branches
This can cause major confusion and broken history.
2. Forgetting Force Push
After rebase, you must push with:
git push --force
(or safer:)
git push --force-with-lease
3. Losing Commits
If something goes wrong:
git reflog
You can recover lost commits.
Mental Model
Think of rebase as:
“Take my changes and pretend I wrote them after the latest version of the branch.”
Conclusion
Git Rebase is a powerful tool that:
- Keeps history clean
- Improves collaboration (when used correctly)
- Helps maintain a professional commit structure
But it must be used with discipline and understanding.
If you master rebase, you move from just “using Git” to truly controlling your project history.
Final Tip
Start using rebase in your local workflow first, then gradually introduce it into team practices with clear guidelines.
Happy coding 🚀
Top comments (0)