The Plain Picture#
You wrote three pages of a group report on Monday, based on Monday's draft. By Wednesday, others have added pages. Instead of stapling your pages on with a note saying "these were written against Monday's version", you rewrite your three pages on top of Wednesday's draft, as if you'd started today. The final report reads as one straight story.
The Real Name#
Rebasing takes the commits on your branch and replays them, one by one, on top of another commit — creating new commits with new hashes (Ch 3: a different parent means a different hash).
before: after `git rebase main` (while on feature):
A───B───C main A───B───C main
\ \
D───E feature D'───E' feature
D and E are replayed on top of C as new commits D' and E'Then main can fast-forward (Ch 5) to include them — giving a perfectly linear history with no merge commits.
Under the Hood — merge vs rebase#
| Merge | Rebase | |
|---|---|---|
| History | shows what really happened, including parallel work | a straight line — easier to read |
| Creates | a merge commit (two parents) | new copies of your commits (new hashes) |
| Rewrites existing commits | no | yes |
| Conflicts | resolved once, in the merge commit | may need resolving per replayed commit |
| Safe on shared branches | always | only on commits nobody else has |
The golden rule of rebasing: never rebase commits that others already have. If you rebase commits you've already pushed to a shared branch (Ch 7), your teammates' copies still point at the old commits, and their history and yours now disagree — a painful mess. Rebase your own, not-yet-shared work.
Common uses:
git pull --rebase— put your local unpushed commits on top of what's new on the server, instead of creating a merge commit for every pull.git rebase -i main— interactive rebase: reorder, reword, or squash (combine) your commits into a tidy set before opening a pull request (Ch 8). It opens an editor listing your commits with a word (pick,squash,reword,drop) in front of each.
If a rebase goes wrong midway: git rebase --abort returns to exactly how things were.
Check yourself
4 questionsWhat does rebasing do to your commits?
Show answerHide answer
Replays them on top of another commit, creating new commits with new hashes.
What's the golden rule of rebasing, and why?
Show answerHide answer
Never rebase commits others already have — it rewrites history they depend on, and their copies diverge from yours.
Merge vs rebase — one advantage of each.
Show answerHide answer
Merge: preserves true history and never rewrites commits. Rebase: clean, linear, easy-to-read history.
What does squashing mean?
Show answerHide answer
Combining several commits into one.