Rebasing
Rebasing is the other way to integrate changes from one branch into another. Instead of creating a merge commit, it replays your commits on top of the other branch, as if you had started your work from its latest commit. The result is a clean, linear history.
Merge vs rebase
Starting point: you're on feature, and main has moved on.
C ─ D feature
/
A ─ B ─ E ─ F mainMerge (git merge main while on feature) keeps the real shape of history and adds a merge commit M:
C ─ D ─ M feature
/ /
A ─ B ─ E ─ F ──┘ mainRebase (git rebase main while on feature) re-creates C and D as new commits C' and D' on top of F:
A ─ B ─ E ─ F ─ C' ─ D' feature
↑
mainTry it
Look at the graph: feature is now a straight line on top of main, and its commits have new ids (they're new commits). Now main can fast-forward:
git switch main
git merge featureType those into the terminal above and watch main slide forward.
The golden rule of rebasing
🚫 Never rebase commits that other people have already based work on (anything pushed to a shared branch like
main).
Rebasing rewrites history: the old commits are replaced with new ones. Anyone who had the old commits now has a conflicting history. Rebasing your own local or feature branch is fine. If you've already pushed it, you'll need git push --force-with-lease.
When to use which
| Use rebase to… | Use merge to… |
|---|---|
Update your feature branch with the latest main |
Integrate a finished feature into main (often via a PR) |
| Keep history linear and readable | Preserve exactly what happened and when |
| Clean up your own commits before a PR | Work with shared branches |
A common team convention: rebase your feature branch on main while working; merge (or squash) the PR into main.
git switch feature
git fetch origin
git rebase origin/main # or: git pull --rebase origin mainMany developers set pull to rebase by default:
git config --global pull.rebase trueRebase conflicts
Because each commit is replayed one by one, you may get conflicts per commit:
git rebase main
# CONFLICT (content): Merge conflict in app.js
# fix the file, then:
git add app.js
git rebase --continue # move on to the next commit
# or give up and go back to how things were:
git rebase --abort(The simulator aborts on rebase conflicts to keep things simple. Practise this flow in a real terminal.)
Interactive rebase: polishing your commits
git rebase -i lets you edit your history before sharing it: squash "fix typo" commits, reword messages, reorder or drop commits.
git rebase -i HEAD~4Your editor opens with:
pick a1b2c3d Add search API
pick d4e5f6a Fix typo
pick 9f8e7d6 Add search UI
pick 1a2b3c4 WIP
# Commands: p(ick), r(eword), e(dit), s(quash), f(ixup), d(rop)Change it to:
pick a1b2c3d Add search API
fixup d4e5f6a Fix typo ← melt into the previous commit, discard its message
pick 9f8e7d6 Add search UI
squash 1a2b3c4 WIP ← combine with the previous commit, edit the messageSave and close, and Git rewrites the commits. Pros use this to turn messy work-in-progress into a clean, reviewable story.
# Even faster: mark a fix for an earlier commit, then auto-squash it
git commit --fixup a1b2c3d
git rebase -i --autosquash mainPractice
- Create
mainwith 2 commits. Branchfeatureand add 2 commits. Go back tomainand add 1 more commit. - Rebase
featureontomainand confirm in the graph that the history is linear. - Fast-forward
maintofeature.
1.What does git rebase main do while you're on feature?
2.Which is the golden rule of rebasing?