Skip to content

Lesson 12 of 14 · 20m

Commits Are Tools, Not Save Points

git bisect only works if every commit builds — atomic, green commits with why-messages turn history into a debugging tool.

Your commit history isn't a backup. It's a toolset, and it's only sharp if you maintain it: git bisect binary-searches it for the commit that broke the build, git log -S 'functionName' is pickaxe-search for when a line appeared or disappeared, git blame names the change behind any line, and git revert undoes a merged commit safely on shared history. Every one of those tools makes the same assumption — that a commit is a coherent, buildable unit.

The wrong mental model is the commit as save point: "WIP", "fix", "more fix", "actually fix this time." A history of WIP commits can't be bisected — you can't ask "which change broke the build" when half the changes don't build.

Why atomic and green matters

git bisect is how kernel regressions get hunted: "worked in 6.1, broke in 6.5" becomes ~log2(commits) test builds, and git bisect run <script> automates it with a command that exits 0 or 1. It only works if every commit bisect lands on actually builds — which is why kernel maintainers enforce "every commit must compile" and reject patch series with mid-series breakage. The rule scales down directly: one logical change per commit, tests green before you commit, refactor separate from behavior change (which is what made the refactoring lesson's commits reviewable at all).

The mechanics are worth knowing cold: git bisect marks commits good or bad and checks out the midpoint each round, and git bisect run <cmd> automates the search with anything that exits 0 or 1 — bisect run npm test finds the breaking commit while you get coffee. The tool is only as sharp as the history it's pointed at: a commit that doesn't build can't be marked, and a commit containing three changes can't be narrowed past "one of these three."

git add -p is the practical tool for keeping commits atomic: it stages hunks interactively, so a messy afternoon of edits becomes three clean commits instead of one "stuff" commit.

The message records the why

The diff already says what changed. The message exists to record what the diff can't: motivation, alternatives you rejected, the issue it fixes. Subject line imperative and short; body answers "why is this the right change" for the developer doing archaeology in two years. Conventional Commits (feat:/fix:) exist to feed changelog and release tooling — the format is for machines, not ritual.

Two commands keep the chain clean mid-work. git commit --fixup <sha> creates a commit explicitly marked as a fix to an earlier one, and git rebase -i --autosquash folds it into place on the next rebase — the repair lands without a manual reorder. And git commit -v prints the diff into the message buffer, which catches "one file too many staged" before the commit exists.

Recovery tools worth knowing before you need them

  • git reflog — every move HEAD made, locally. Run it after a bad reset or a rebase gone wrong and the "lost" commit is sitting right there; reflog has saved more work than any other single command.
  • git revert vs git reset — revert adds an inverse commit and is safe on anything pushed; reset rewrites history and belongs strictly to unpushed work.
  • git rebase -i — clean up the WIP chain before pushing, not after.
  • Squash merges — when the feature branch's history is genuinely noise, squash-merge keeps the main branch's history bisectable.

The rule underneath all four: rewrite history freely until it's shared; after it's pushed, history is append-only.

Try it yourself — bisect narrows fast, in JS

// log2(1000) ≈ 10: ten builds find the bad commit in a thousand
const commits = 1000;
console.log("bisect builds needed:", Math.ceil(Math.log2(commits)));
// ...but only if every commit it lands on actually builds.

The practice

One logical change per commit, always green, message records why — the diff shows the what on its own. You aren't writing history for neatness; you're building the debugging tool your future self will run at 2am, one bisectable commit at a time.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.