Git rebase explained: understand and use rebase
Rebase is one of the most powerful and most misunderstood Git commands. This page explains everything: how it works, the difference with merge, interactive rebase and best practices.
What is git rebase?
git rebase takes the commits from your branch and replays them on top of another branch, as if you had started your work from the most recent version.
The result: a linear history, without the merge commits that clutter the log. It's like putting your changes "at the end of the line" instead of creating a fork.
The basic syntax: git rebase main from your feature branch. Your commits are recreated with new identifiers, but the content remains the same.
Git rebase syntax
git rebase <target-branch>Replay your commits on the target branch
git rebase -i HEAD~nInteractive rebase on the last n commits
git rebase --abortAbort an in-progress rebase
git rebase --continueContinue after resolving a conflict
Git rebase in action
Two concrete examples: a simple rebase and an interactive rebase.
Simple rebase
Interactive rebase
Git merge vs git rebase: what is the difference?
git merge
- Preserves complete history (who did what, when)
- Safe on shared branches
- Creates an explicit merge commit
- Non-linear history, harder to read
- Merge commits that "pollute" the log
git rebase
- Linear, clean and readable history
- No unnecessary merge commits
- Ideal for cleaning up before a final merge
- Rewrites history (new commit hashes)
- Dangerous on branches already shared
Rebase best practices
Never rebase a public branch
If other people are working on the branch, use merge. Rebase rewrites history and creates impossible conflicts for others.
Rebase your feature branch before merging
Before merging your branch into main, run git rebase main to integrate the latest changes and get a clean history.
Use interactive rebase to clean up
Before sharing your work, squash small fix commits with squash and clarify messages with reword.
When in doubt, use merge
Merge is always safe. If you're not sure about the situation, merge is the safest choice. You can always adopt rebase later.
Part of the Git Branching Workflow guide
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
Questions about git rebase
Practice rebase in a safe environment
The GitQuest simulator lets you test merge and rebase without risk. Ideal for understanding the difference.
Try it in the terminal