Topic 3 of 5 · 4 min read
Finding the breaking commit with bisect
Goal: Run git bisect with a pass/fail command to find the agent commit that introduced a regression in a range of fifty commits.
When something broke somewhere in a long run of commits, give git a command that exits 0 for good and non-zero for bad, and let git bisect run find the first bad commit by binary search. Fifty commits take about six test runs, not fifty.
Check yourself · 1 of 3
What does git bisect run need in order to work without your help?
Why it matters
Agents make many small commits. When a regression shows up after an overnight run, reading fifty diffs is slow and you may blame the wrong one. Bisect turns the question "which commit did this?" into a mechanical answer, as long as you can state "broken" as a command.
How binary search helps
Bisect checks out the middle commit of the range and asks: good or bad? Each answer halves the range. Fifty commits need at most six steps, because two to the power of six is 64.
How to do it
- Find a bad and a good commit. Bad is usually
HEAD. Good is a tag, a release, or the last commit you know worked. - Write a check that exits 0 or non-zero. A single test, a build, or a small script. It must be quick and deterministic.
- Run it.
git bisect start
git bisect bad HEAD
git bisect good v2.3.0
git bisect run npm test -- tests/invoice.test.ts
# ... prints: 4f2a9c1 is the first bad commit
git bisect reset
Exit codes matter. In bisect run, 0 means good, 1 to 127 (except 125) means bad, and 125 means "cannot test this commit, skip it". Use 125 for commits that do not build. Exit codes above 127 abort the whole run.
- Inspect the result.
git show <hash>shows the change, and its message may name which agent or task made it. - Always
git bisect resetto return to your branch.
Before and after
Before: invoice totals are wrong in the nightly build. You open the last twenty diffs and suspect a rounding change, which was fine.
After: a script check-invoice.sh runs one failing test. Bisect names a commit titled "Refactor tax helper" that changed a rounding mode. Five runs, two minutes.
What goes wrong
- A flaky check. If the test passes sometimes on a bad commit, bisect lands on the wrong commit. Run it twice, or fix the flake first.
- A check that tests the wrong thing. Make sure it fails for the reason you care about, not because the build broke in the middle of the range.
- Dirty working tree. Bisect checks out other commits. Commit or stash first, and use a worktree so you do not disturb others.
- Squashed or huge commits. Bisect points at one commit; if it holds a thousand lines, you still must dig.
Examples
A check script with skip
#!/usr/bin/env bash
# check-invoice.sh
npm run build >/dev/null 2>&1 || exit 125 # cannot test: skip
npm test -- tests/invoice.test.ts # 0 good, 1 bad
git bisect run ./check-invoice.sh skips commits that do not build and judges the rest by the invoice test.
Reading the result
4f2a9c1e3b is the first bad commit
commit 4f2a9c1e3b
Author: codex-agent
Refactor tax helper
The next step is git show 4f2a9c1e3b, then a fix or a revert with git revert 4f2a9c1e3b.
Key points
- git bisect run needs a command whose exit code says good (0) or bad (non-zero).
- Binary search finds the culprit in about log2 of the number of commits, so fifty commits need at most six runs.
- Exit code 125 tells bisect to skip a commit that cannot be tested, such as one that does not build.
- Flaky or mis-aimed checks send bisect to the wrong commit, so make the check deterministic.
- Always finish with git bisect reset, and run it in a worktree so you do not disturb other work.
Common mistakes
- Using a flaky test as the check, so bisect blames an innocent commit.
- Forgetting to treat unbuildable commits as skip (125), so build failures read as the bug.
- Bisecting in a dirty folder that other agents or people are using.
- Not running git bisect reset and then committing on a detached checkout.
Check yourself · 2 of 3
About how many test runs does bisect need to find one bad commit among fifty?
Check yourself · 3 of 3
During bisect run, one commit fails to compile. Which exit code should your script return for it?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The free reference book for branching, rebasing, reset and recovery, written by Git's maintainers' community.
- git-worktree documentation (opens in a new tab)DocsFree
How to give each task its own checkout of one repository, and how to list, lock and remove them.
- git-bisect documentation (opens in a new tab)DocsFree
Exact syntax for bisect start, good, bad, run and reset, with exit-code rules for scripts.