Sub topic 8
Why bisect works: binary search over history
This chapter explains the idea behind git bisect. It assumes you can read git log output and know that a commit is a snapshot with a parent and a hash. The next chapter shows the commands. Here you learn why the method is fast and when it can mislead you.
The problem bisect solves
A bug appears in your project. It worked at release v1.2.0, and it is broken now. Hundreds of commits sit between those two points, and any of them could be the cause.
You could check out each commit from oldest to newest and test it. That is a linear search, and it takes as many tests as there are commits. If each test needs a build and a test run, this is slow and tedious.
A regression is a behavior that used to work and now fails. Bisect finds the commit that introduced a regression, and it does so with far fewer tests.
Binary search in one paragraph
Binary search finds an item in a sorted list. You look at the middle element and decide which half holds your target. You discard the other half and repeat. Each step halves the remaining candidates.
History works the same way when you can sort commits into two groups: "good" commits before the bug and "bad" commits after it. Bisect picks a commit near the middle of the range, and you test it. A good result moves the lower boundary up. A bad result moves the upper boundary down. The first bad commit is the one right after the last good commit.
How fast it is
The number of tests grows with the logarithm (base 2) of the commit count, rounded up. Doubling the history adds only one more test.
| Commits in range | Tests needed (at most) |
|---|---|
| 10 | 4 |
| 100 | 7 |
| 1,000 | 10 |
| 100,000 | 17 |
You can count the commits in your own range with git rev-list:
git rev-list --count v1.2.0..HEADThis prints the number of commits reachable from HEAD but not from v1.2.0. That number is your n.
What bisect needs from you
Bisect gives correct answers only when its assumptions hold.
- A known good commit. This is an older commit where the bug is absent, such as a release tag.
- A known bad commit. This is usually
HEAD, where you can see the bug. - A bug that stays once introduced. The history should look like good, good, good, bad, bad, bad. If the bug appears, disappears, and returns, the "sorted list" is not sorted, and the search can land on the wrong commit.
- A repeatable test. Each commit must give the same answer every time you check it.
Branches and merges
Real history is a graph, not a straight line, because merges join branches. Bisect still works. It considers the commits that are reachable from the bad commit but not from the good one. At each step it picks the commit that splits that set as evenly as it can. The result is not always exactly the middle by position, but the halving effect is the same.
If the first bad commit is a merge commit, the bug probably comes from the way two branches interact, and neither branch alone is at fault.
Common failure modes
- Flaky tests. A test that sometimes passes and sometimes fails will send bisect down the wrong path. A single wrong answer corrupts every later step. Run the check several times, or fix the flakiness first.
- Wrong endpoints. If your "good" commit is not actually good, bisect reports a commit that did not introduce the bug. Verify both endpoints before you start.
- Commits that cannot be tested. Some commits do not build, for example in the middle of a refactor. You cannot call them good or bad. Git has a way to skip them, covered in a later chapter.
- A different bug. The failure you see at
HEADmay have a different cause than the failure at some older commit. Make sure your test checks the specific symptom you care about.
What happens during a bisect
Bisect checks out a commit in the middle of the range, so your working tree changes to match it. It does not rewrite history or create commits. When you finish, you return to your original branch with git bisect reset. Because bisect checks out other commits, stash or commit any uncommitted work first. The earlier stash chapters cover how.
Exercise
Your range between a known good tag and HEAD holds 600 commits. About how many tests does bisect need at most, and why?
Show answer
At most 10 tests. Each test halves the remaining candidates, so you need the smallest k where 2^k is at least 600. Since 2^9 = 512 is too small and 2^10 = 1024 is enough, k is 10.
Exercise
A test fails about one run in five on commits that are actually good. What goes wrong if you answer bisect from a single run, and what can you do about it?
Show answer
A single run may report "bad" for a good commit. Bisect then discards the wrong half of the range and returns a wrong first bad commit. Run the test several times per commit, or stabilize the test before you bisect.