How can ten tests find one bad commit in a thousand?
In short
git bisect tests the middle commit and drops the half that cannot hold the bug. Ten halvings cover a thousand commits, if the test answers the same way.
One commit in a thousand broke the build. So how can ten tests find it? git bisect does it by playing higher or lower with your history. This post runs that game for real: once with a steady test, twenty times with a flaky one, and once with a plain number from 1 to 100.
The repository, its 1,000 commits and the number 73 are made up. The runs are real: git 2.55.0, and every number below comes from their output.
A thousand suspects, one culprit
The git documentation describes git bisect in one line: "Use binary search to find the commit that introduced a bug." You give it one commit that is known to be bad and one that is known to be good. It then picks a commit in the middle of that range and asks whether it is good or bad.
The answer decides which half stays. If the middle commit works, the bug came later, so the earlier half is gone. If it is broken, the bug is in that commit or before it, so the later half is gone. git keeps asking until one commit is left.
The test repository has 1,000 commits. From commit 737 on, a file says bug instead of ok. The test reads that file.
The steady run: ten tests
The test passes on ok and fails on bug. Started on commit 1 (good) and commit 1,000 (bad), git printed:
Bisecting: 499 revisions left to test after this (roughly 9 steps)
The run then tested these commits, in this order:
500 pass 750 fail 625 pass 687 pass 718 pass734 pass 742 fail 738 fail 736 pass 737 failtests: 10git names: commit 737
Ten tests, and git named the right commit. git's own estimate matches: the first test plus "roughly 9 steps". The reason is the arithmetic of halving: 2 to the power of 10 is 1,024, so ten halvings are enough for a thousand commits. The documentation's own example shows the same scale, with "675 revisions left to test after this (roughly 10 steps)".
Let a script answer
You can type git bisect good and git bisect bad by hand. With git bisect run, a script answers instead. The documentation sets the rules: the script exits with 0 if the code is good, and with a code from 1 to 127 if it is bad, except 125. Exit code 125 means the current code "cannot be tested", and git skips that commit.
git bisect start <bad-commit> <good-commit>git bisect run ./test.shgit bisect reset
git bisect reset ends the session and returns to the commit you were on before you started. In the runs here,
test.sh is the file check described above.
A flaky test: one lucky pass
Now the same search with a flaky test. This one is made up for the run: on a working commit it passes, on a broken commit it passes by luck half the time. The coin is a random number with a fixed seed, so each run can be repeated.
Run 8 of twenty shows the smallest possible slip. Every answer is right except one:
500 pass 750 fail 625 pass 687 pass 718 pass734 pass 742 pass (broken, lucky) 746 fail 744 fail 743 failtests: 10git names: commit 743
Commit 742 is broken, but the test passed it. git took that answer as true: the bug must come after 742. Commits 737 to 742 were dropped with that half. Every later answer was honest, and the search still ended on 743, six commits too late.
Across all twenty flaky runs:
flaky runs: 20runs with at least one lucky pass: 18of those, named commit 737: 0runs that named commit 737: 2 (no lucky pass happened)
git never warned. Every run ended the same way, with one commit named as the first bad one. The reel shows another flaky run, in which git named commit 832; the twenty runs here named other commits, because each run flips different coins.
Why one lucky pass is enough
The same game with a number shows why. The secret is 73, somewhere from 1 to 100. Each guess is the middle of what is left, and the answer is "higher" or "lower".
honest answers: 50 75 62 68 71 73 found in 6 guesses"lower" at 50, once: 50 25 37 43 46 48 49 found nothing
With honest answers, six guesses find 73. No number from 1 to 100 needs more than seven, because 2 to the power of 7 is 128.
Now answer "lower" at 50, just once. Every answer after that is true: 73 is higher than 25, higher than 37, and so on. But the game never goes back to a half it dropped. After the one wrong answer it only searches 1 to 49, so it ends on nothing.
That is the flaky run in small. Halving is fast because it throws half away for good, and for the same reason it cannot recover from one wrong answer.
Check it yourself
The run's scripts are short. This is the core of the steady test, run inside a repository where a file named
state holds ok or bug:
#!/usr/bin/env bashgrep -q ok state
grep -q exits with 0 when it finds ok and with 1 when it does not, which is exactly what
git bisect run expects.
Before you start a bisect with your own test, run that test a few times on the known bad commit. If it ever passes there, it can pass a broken commit in the middle too, and the search will end on the wrong commit without saying so.
What it does not do
-
Ten tests is a count for a straight history. The test repository has no merges. A history with many merges was not tested here. For regressions that come in through a merged branch, the documentation offers
--first-parent, which follows only the first parent of each merge commit. - It cannot test what does not build. Exit code 125 skips a commit. The documentation warns that if you skip a commit next to the one you are looking for, git cannot tell exactly which of them was the first bad one.
- The flaky test was built for this example. It passes a broken commit half the time. A test that fails at random in other ways would push the search elsewhere.
- The repository is made up. 1,000 commits and a bug at 737 are chosen for the example. The runs and their output are real.
- git trusts every answer. It names one commit at the end of a flaky run just as it does at the end of a steady one.
Get the next explainer
Every reel on this site gets a post like this one: the example run, with sources and limits. Until the next one: ten tests find the bad commit, as long as the test answers the same way every time.
If you would like the next post by email, sign up below. You will first get an email with a confirmation link; you are only on the list after you click it, and every email has a link to unsubscribe.
Sources
-
git documentation, "git-bisect": binary search, good and bad commits, the commit in the middle, the step estimate,
git bisect runand its exit codes,skip,--first-parent,reset.
Source last opened on 1 December 2026. The runs (one steady, twenty flaky, and the number game) ran on 1 December 2026 with git 2.55.0 and Python 3.
More posts
-
How does an API key end up in a git push, and what keeps it out?
A saved permission rule can hold a command with its key, and .claude/settings.json is meant to be committed. Where the key's value can live instead.
-
What does npm install run before you open a single file?
npm install runs the scripts listed in the folder's own package.json. npm 12 blocks those of downloaded packages until that same file approves them.
-
The package didn't exist, so why was the failed install the better one?
Coding models make up package names, and many come back every time. Once someone registers one, the install and a list check both pass.