Sub topic 1
Why binary search finds a regression quickly
A regression is a behavior that used to work and now doesn't. You usually know a commit where it worked, such as a release tag, and a commit where it fails, such as the tip of main. The cause is somewhere in between. This chapter explains why git bisect can locate that commit in a handful of tests, even when the range holds thousands of commits. It also covers the assumptions that make this work and the cases where they break.
Prerequisites
This chapter assumes you:
- Are comfortable with basic Git: commits, branches, tags, and
git checkout. - Can read and write simple shell scripts.
- Can run your project's build and test commands from the command line.
You don't need to know git bisect yet. Later chapters cover the commands in detail. This one builds the mental model that the rest relies on.
The problem: a long range with one change inside
Suppose version 1.4.0 passed your regression test and main fails it. Between them are 1,000 commits. Any of them could be responsible.
The obvious approach is a linear search. You check out commits one at a time, build, run the test, and stop at the first failure. If you start from the good end, you test about 500 commits on average and up to 1,000 in the worst case. If each build and test takes two minutes, the worst case is more than 33 hours.
Binary search avoids this by exploiting one fact: the history is ordered, and the test result changes exactly once along it.
How binary search narrows the range
Binary search tests the commit in the middle of the range, and the answer tells you which half to discard.
- If the middle commit is good, the bug was introduced after it. Discard everything up to and including that commit.
- If the middle commit is bad, the bug was introduced at or before it. Discard everything after it.
Each test halves the number of candidates. The search ends when one candidate is left. That candidate is the first bad commit.
The cost: about log2(N) tests
If you halve N candidates repeatedly, the number of halvings needed to reach one candidate is the base-2 logarithm of N, rounded up. A range of 1,000 commits needs 10 tests, because 2^10 = 1,024 is the smallest power of two that covers 1,000.
| Commits in range | Tests with binary search | Worst case, linear search |
|---|---|---|
| 10 | 4 | 10 |
| 100 | 7 | 100 |
| 1,000 | 10 | 1,000 |
| 10,000 | 14 | 10,000 |
| 100,000 | 17 | 100,000 |
| 1,000,000 | 20 | 1,000,000 |
The key property is that the cost grows very slowly. Multiplying the range by 1,000 adds only about 10 tests. A range that would take days to search linearly takes minutes, and the two-minute test from the earlier example now costs about 20 minutes in total instead of 33 hours.
You can compute the number for your own range. First count the commits between the good and bad endpoints:
git rev-list --count v1.4.0..HEADThen take the base-2 logarithm and round up:
python3 -c "import math; print(math.ceil(math.log2(1000)))"This prints 10. Replace 1000 with your own count.
What Git does for you
Doing this by hand means tracking the range, picking midpoints, and checking them out. git bisect does that bookkeeping. You give it one good commit and one bad commit. Git then checks out a commit near the middle of the range and waits for your verdict. You answer with git bisect good or git bisect bad, and it repeats until it can name the first bad commit.
The following sketch shows the shape of a session. Chapter 2 walks through it step by step, so don't worry about the details yet.
git bisect start
git bisect bad HEAD
git bisect good v1.4.0Git responds with a message like this:
Bisecting: 499 revisions left to test after this (roughly 9 steps)The "roughly 9 steps" figure is Git's own estimate of the remaining tests, which is the log2 calculation applied for you. The 1,000-commit example needs about 10 tests in total, and one of those is the commit Git has just checked out.
Two details are worth knowing:
- Git picks the midpoint by counting commits, not by looking at dates. A commit made last week isn't "closer" to the bad end than one made last year unless it sits later in the commit graph.
- Each verdict is a question about one commit. The test must run on exactly that commit's files, so your build must reflect the checked-out state.
git bisect run automates the last part. Instead of you typing good or bad after each test, a script reports the verdict. The following chapters build up to that.
Assumptions the search depends on
Binary search is only correct when the data has a particular shape. For bisecting, three assumptions have to hold.
- There is one transition from good to bad. Everything before the culprit commit passes, and everything after it fails.
- The result at a commit is repeatable. The same commit tested twice gives the same verdict.
- Each tested commit can be evaluated. You can build and run it.
When these hold, the middle-commit test is a reliable signal and each step discards the right half.
When there is more than one transition
Suppose a bug is introduced in commit 300, fixed in commit 600, and reintroduced in commit 800. The test is bad at 1,000, good at 1. Bisect can only find one good-to-bad boundary. It might report commit 300, or commit 800, depending on which midpoints it happens to test. Both are real answers, but you may only want one of them.
Mitigation: narrow your endpoints. If you care about the latest breakage, use a good commit from after the fix as your good endpoint.
When the result is not repeatable
A flaky test, which sometimes passes and sometimes fails on the same commit, breaks assumption 2. One wrong verdict sends the search into the wrong half. From then on every step is plausible but misleading, and the final answer is a commit unrelated to the bug. Git cannot detect this. A later chapter covers strategies such as running the test several times per commit.
When a commit cannot be tested
Some commits don't build, perhaps because a developer committed a half-finished change. You can't answer "good" or "bad" for them honestly. Marking them bad may move the boundary to the wrong place. Git has a way to skip such commits, and the script contract for that comes in a later chapter.
Non-linear history
Real repositories contain merges, so the history is a graph rather than a straight line. Binary search still works. Git picks a commit that splits the set of remaining candidate commits as evenly as it can, counting the commits reachable from the bad endpoint but not from the good one. The range is still reduced by roughly half at each step, so the log2(N) estimate holds approximately.
The result can be surprising, though. The first bad commit may sit on a side branch. If your project's main line is made of merge commits and you want the merge that brought in the bug, a later chapter shows the --first-parent option.
Choosing good endpoints
The cost depends on how wide the range is, so the endpoints matter less than people expect. Moving the good endpoint back by a factor of ten adds only about three tests. This means you should be generous. Pick a good commit you are confident about, such as a tagged release, instead of spending an hour guessing a tighter bound.
The one risk of being generous is a wider range that includes unrelated problems, such as a period when the test was broken for another reason. If the first bad commit looks unrelated to your bug, check whether your range includes such a period.
Summary of the model
- A regression has a last good state and a first bad state, and the history between them is ordered.
- Testing the middle commit discards half the candidates, whatever the verdict.
- N commits need about log2(N) tests, so 1,000 commits take about 10 tests.
- The method requires a single, repeatable, testable good-to-bad transition.
- Flaky tests, repeated regressions, and unbuildable commits are the main ways it goes wrong.
Exercise
A release tag v2.0.0 passes your regression test and the current tip fails. git rev-list --count v2.0.0..HEAD prints 5000. Roughly how many tests will a bisect need in the worst case, and how does that compare to a linear search? Show the calculation.
Show answer
log2(5000) is about 12.3, so you round up to 13 tests. Check: 2^12 = 4,096 is less than 5,000, and 2^13 = 8,192 is enough. A linear search needs up to 5,000 tests. Bisect is therefore about 380 times cheaper in the worst case.
Exercise
A test fails at commit 1,000 and passes at commit 1. In reality, the bug was introduced at commit 300, fixed at commit 600, and reintroduced at commit 800. Which assumption of binary search does this break, and what can you do to get a useful answer?
Show answer
It breaks the assumption of a single good-to-bad transition. Bisect can report either commit 300 or commit 800, depending on the midpoints it tests. To target the latest breakage, choose a good endpoint after the fix, such as a commit between 600 and 800 that you have confirmed passes the test.