Rebase Basics
Introduction
Rebase replays your branch commits on top of another branch tip—often producing a linear history without merge commits. It complements merge but has strict rules on shared branches. This chapter runs git rebase main, compares rebase vs merge, and introduces interactive squash as an advanced option.
Prerequisites
- Merging Branches
- Resolving Merge Conflicts
- Feature branch with commits not on
main
Visual: Merge vs Rebase
Before—main and feature diverged:
main: A --- B --- C
\
feature: D --- EAfter merge on main:
main: A --- B --- C ------- M
\ /
feature: D --- E ------After rebase feature onto main (then fast-forward merge):
main: A --- B --- C --- D' --- E'
feature: (same tip as E')Code explanation:
D'andE'are new commit IDs—same changes, new parents
Rebase Feature Branch Onto main
Update local main:
git switch main
git pull origin mainRebase feature:
git switch feature/login
git rebase mainIf clean, feature history sits directly after C.
Merge into main (often fast-forward):
git switch main
git merge feature/loginHandle Conflicts During Rebase
Git pauses at each conflicting commit:
# fix files, remove conflict markers
git add conflicted.js
git rebase --continueSkip current commit (rare):
git rebase --skipAbort entire rebase:
git rebase --abortCode explanation:
- Similar to merge conflicts—resolve, stage, continue
Golden Rule
Warning
Never Rebase Public History
Do not rebase commits that others already pulled—rewritten SHAs break their clones. Rebase your local feature branch before PR; do not rebase main after push without team agreement.
Safe pattern:
- You alone work on
feature/x git rebase mainlocallygit push --force-with-leaseto update PR branch (only your feature)
Never force-push shared main.
rebase vs merge Summary
| merge | rebase | |
|---|---|---|
| History shape | Branches visible | Straighter line |
| Commit IDs | Preserved | Rewritten on feature |
| Shared branch safety | High | Low if already public |
| Learning curve | Lower | Medium |
Teams pick one style—or merge to main, rebase locally only.
Interactive Rebase (Squash)
Combine last 3 commits on feature branch:
git rebase -i HEAD~3Editor lists commits with commands:
pick a1b2c3d Add login form
squash e4f5g6h Fix typo
squash f6g7h8i Fix typo againSave—Git merges messages into one commit.
Code explanation:
pickkeeps commit as-issquashfolds into previous commit- Use before PR for clean review—only on unpushed or personal branch
Pull With Rebase
Integrate remote main without merge commit on your branch:
git pull --rebase origin mainOr set default:
git config --global pull.rebase trueFAQ
Rebase deleted my commits?
They are orphaned until garbage collected—git reflog finds old tips.
PR says "needs rebase"?
Platform wants branch updated on latest main—rebase or merge main into feature locally, push.
rebase vs merge conflict count?
Rebase may conflict per replayed commit; merge often one conflict at merge point.
Can I rebase onto remote without fetch?
Run git fetch first—rebase needs accurate origin/main.
GitHub "Rebase and merge" button?
Platform rebases your commits onto base—similar effect, done server-side.
Interactive rebase scared me?
Use git rebase --abort—returns to pre-rebase state.