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 logandgit 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:
bfac2bd steps 5fdb82e more 2412563 wip 9536c25 Say where the photos are 762aa94 forgot eggs fa12281 ingredients 572d243 bibingka
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
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
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"
Joaquin says: “Why bother tidying? The recipes end up the same either way. Just push the wips.”
What happens if…
Check what you learned
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.
- Pro Git: Rewriting History: why you'd rewrite, and the rule about pushed work
- Git documentation: git-rebase: rebasing makes new commits
- How to Write a Git Commit Message (Chris Beams): why well-made history pays off