Rewriting history · lesson 1 of 6
Commands and ideas in this lesson
AHistory as documentationwhy tidy matters
BRewritingalways makes new commits

Why history is worth tidying

Last updated

When you're working, commits come out messy: "wip", "forgot eggs", "more". That's fine while you work. Before others read your work, it's worth tidying, because history is read far more often than it's written. In this lesson you'll see why, and the one fact that makes every tidying command in this stage make sense.

By the end of this lesson you can

  • Explain why a tidy history helps reviewers and future readers
  • Explain that Git never edits a commit: rewriting makes new commits
  • Say which commits are safe to rewrite

History is read more than written

You write a commit once. It's read many times:

  • by reviewers, commit by commit, in a pull request;
  • by you, months later, asking "why does the adobo say this?" with git log and git blame (lesson 8.5);
  • by git bisect, which finds the commit that broke something by testing commits one at a time (lesson 8.4). It only works if each commit is a sensible step;
  • by anyone who needs to undo one change with git revert, which is easy when that change is one commit and hard when it's spread over five.

Small commits, each one complete change with a clear message, make all of that easy.

Messy and tidy, side by side

The same bibingka work, as Ana made it, and tidied before her pull request:

As made
bfac2bd steps
5fdb82e more
2412563 wip
9536c25 Say where the photos are
762aa94 forgot eggs
fa12281 ingredients
572d243 bibingka
Tidied
3c1e7a0 Say where the photos are
8d4b2f1 Add bibingka recipe

The tidy version has one commit for the recipe and one for the unrelated README change, so either can be reviewed, reverted or found on its own. Nothing was lost: the recipe ends up exactly the same. Lesson 6.4 does this tidy-up.

Commits are copied, never edited

Every commit's hash comes from everything in it: the files, the message, the author, the date, and its parent's hash. Change any of those and it's a different commit with a different hash.

So Git can't edit a commit. Every command that "rewrites" history (commit --amend, rebase, rebase -i, cherry-pick) makes new commits, and moves the branch label to them. The old ones are still there, unlabelled, until Git cleans up weeks later; the reflog can find them (lesson 2.7).

$ git log --oneline -1
67e435d (HEAD -> main) Cover the leche flan with foil
  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 3 The newest commit, “Cover the leche flan with foil”, has its hash.

That's why the one rule of Stage 6 is about who else has the old commits (lesson 6.3). Rewriting commits only you have is safe. Rewriting commits others have built on makes their copies and yours disagree.

Spot the trap: "only the final code matters"

Spot the trapJoaquin

Joaquin says: “Why bother tidying? The recipes end up the same either way. Just push the wips.”

What happens if…

Check what you learned

What happens to a commit when you “rewrite” it, for example with --amend?
Which commits are safe to rewrite?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git