Sub topic 1
Why Binary Search Finds a Regression Quickly
Prerequisites
This chapter assumes you can:
- Use basic Git: commits, branches, tags, and
git checkout. - Read and write simple shell scripts.
- Run your project's build and test commands from the command line.
You do not need to know git bisect yet. This chapter explains the idea behind it. Later chapters cover the commands.
The problem: a regression somewhere in the history
A regression is a bug in something that used to work. You know a tag such as v1.4.0 was fine. You know main is broken now. Between them sit many commits, and one of them introduced the bug.
You could check each commit one at a time, starting at v1.4.0 and moving forward. With 1,000 commits, that could take 1,000 builds and test runs. You could also read every diff and guess. Both approaches are slow and error-prone.
The idea: halve the range at each step
Binary search finds an item in an ordered list by testing the middle and discarding the half that cannot contain the answer. Git history is ordered in the way this needs. Every commit after the bug appeared is also bad, so the commits look like this:
- Good, good, good, good, bad, bad, bad, bad
The boundary between the last good commit and the first bad commit is what you want to find. Git calls the first bad commit the culprit.
The search works like this:
- Mark one commit as good and one as bad.
- Git checks out the commit halfway between them.
- You test it. If it is good, the bug is in the later half. If it is bad, the bug is in the earlier half (or is this commit itself).
- Repeat on the smaller range until one commit remains.
How many tests it takes
Each test halves the range, so the number of tests grows with the base-2 logarithm of the number of commits, written log2(N). In practice, you need about ceil(log2(N)) tests, where ceil rounds up to a whole number.
| Commits in range (N) | Tests needed (about) |
|---|---|
| 8 | 3 |
| 100 | 7 |
| 1,000 | 10 |
| 10,000 | 14 |
| 1,000,000 | 20 |
Going from 1,000 commits to 10,000 adds only four tests. This is why bisecting is practical even on very large repositories. The cost of each test is what dominates, which is why later chapters automate it.
What Git does for you
You do not compute midpoints yourself. Git does the following:
- It tracks the known good and bad commits.
- It picks the next commit to test and checks it out.
- It reports how many steps remain, roughly.
- It prints the first bad commit when the range shrinks to one.
The command that starts this process is git bisect start. A short preview, which the next chapter walks through in detail:
git bisect start HEAD v1.4.0Here HEAD is the bad commit and v1.4.0 is the good one. Git responds with the number of commits left to test and checks out a middle commit.
When the assumption breaks
Binary search relies on one rule: once the history turns bad, it stays bad. Three situations can violate that rule.
- Flaky tests. A test that fails at random can mislead the search. One wrong answer sends Git into the wrong half.
- Untestable commits. A commit that does not build tells you nothing about the bug. Git needs a way to skip it.
- Merges. History is a graph, not a line. Git handles this, but the commit it reports may need extra interpretation.
Later chapters cover each of these.
Exercise
A repository has 500 commits between a known good tag and a known bad commit. About how many tests does a bisect need to find the first bad commit? Why does doubling the range to 1,000 commits add only one test?
Show answer
About 9 tests, since 2^9 = 512 is the smallest power of two that is at least 500. Each test halves the remaining range, so doubling the number of commits requires only one extra halving. 1,000 commits need about 10 tests.