Definition
What it is
git rebase main in a feature branch does three things. First, it finds the common ancestor of the feature branch and main. Second, it stores the feature branch's commits from that ancestor. Third, it moves the feature branch onto main's tip and replays the stored commits one by one. Each replayed commit gets a new hash, a new parent, and possibly a resolved conflict.
The result is a linear history: as if the developer had written every commit after the latest main. Merge commits disappear. The history looks like main → f1 → f2 → f3, not main ↘ f1 → f2 → f3 ↗ with a merge bubble.
Why it matters
A linear history is easier to read (no zig-zags), easier to bisect (no branching), and easier to review (git log --oneline shows the actual sequence of work). For projects with hundreds of commits a day, the noise of merge commits from two-line branches is significant.
The cost is real: rebase rewrites history, so it breaks every clone that has pulled the branch before the rebase. That is why the rule is "rebase only local, never shared branches".
Rebase vs merge
Aspect | Merge | Rebase |
|---|---|---|
History shape | Bubble (two parents) | Linear |
Hashes of commits | Preserved | Rewritten |
Safe on shared branches | Yes | No |
Conflicts | Once at merge | Per replayed commit |
Audit trail | Full | Simplified |
Typical use | main ← feature | feature → main |
The rebase replay
graph LR C1[c1] --> C2[c2] C2 --> C3[c3 main] C1 --> F1[f1] F1 --> F2[f2] F2 --> F3[f3] C3 -.rebase.-> R1[f1] C3 -.rebase.-> R2[f2] R2 --> R3[f3] style C3 fill:#E8F5E9 style R3 fill:#FFF3E0
When to rebase
Rebasing without breaking the team
Local branches only
The rule is simple: never rebase a branch others have pulled. If you already pushed, merge or force-with-lease.
Interactive rebase before PR
git rebase -i mainlets you squash fixups, reorder commits, and rewrite messages. Do it before pushing.Use `--force-with-lease`
If you must force push after rebase, use
--force-with-leaseinstead of--force; it fails if someone else pushed.Rebase before merging feature to main
Keeps the main history linear: your commits land on top of main, not as a bubble.
Never rebase main
Rewriting main breaks everyone. Merge into main, never rebase main.
Common failures
Origin
The term is literal: to re-base a series of commits, change its base. Git introduced rebase in 2005 as part of its initial design. Older systems (CVS, Subversion) had no equivalent: they could not rewrite history because their history was a linear chain. Git's DAG of commits made rebase possible. The term spread to other DVCS: Mercurial added hg rebase in 2008 as an extension, then as a built-in.
Rebase re-applies the commits on the current branch on top of the specified branch. The result is a new series of commits, each with a new hash.
— Chacon & Straub, *Pro Git*, 2014 (paraphrase)
Evidence
FAQ
- When is rebase safe?
Only on branches nobody else has pulled. Rule of thumb: rebase only local, unpushed commits.
- How to force push after rebase?
git push --force-with-lease, never plain--force. It aborts if the remote has moved.- Rebase vs merge for PRs?
Rebase before opening a PR (linear history for review). Merge with
--no-fffor the PR itself if you want an audit trail.- Can I rebase main?
No. Rewriting main breaks every clone. Merge into main.
Connections
Chacon, S. & Straub, B. (2014). Pro Git, Chapter 3. https://git-scm.com/book/en/v2/Git-Branching-Rebasing
Git project (2024). git-rebase(1). https://git-scm.com/docs/git-rebase
Atlassian (2024). Merging vs rebasing. https://www.atlassian.com/git/tutorials/merging-vs-rebasing
Etymology
Origin: compound
re(prefix): Latin, "again"base(noun): Greek basis, "foundation, pedestal"
Semantic network
● broader ● narrower ● related
Sources
Chacon, S. & Straub, B. (2014). Pro Git. https://git-scm.com/book/en/v2/Git-Branching-Rebasing
Git project (2024). git-rebase(1). https://git-scm.com/docs/git-rebase
Atlassian (2024). Merging vs rebasing. https://www.atlassian.com/git/tutorials/merging-vs-rebasing