Notes on a tool that most developers know exists and few actually use. Opinion up front: if you have a regression and a repository with history, bisecting is usually faster than reading code. People skip it because reading code feels like progress. Bisecting is progress with arithmetic behind it.

The idea

You know a version where the feature worked. You know a version where it is broken. Somewhere between them is the commit that broke it. Say there are 1,000 commits in between. Check out the one in the middle and test. If it is broken, the culprit is in the first half. If it works, the culprit is in the second half. Either way you have thrown away 500 commits with one test. Repeat.

This is binary search, applied to history instead of a sorted array. Because each step halves the range, 1,000 commits need at most 10 tests, since 2 to the power of 10 is 1,024. A range of 5,000 commits needs at most 13. Going from a thousand suspects to one in ten tests is the whole appeal. Scale does not hurt: ten times the commits costs only three or four extra steps.

How it runs

You start a session with git bisect, tell it one bad commit (usually the current one) and one good commit (a release tag, a date you are sure about). Git checks out a commit in the middle. You build, run your check, and report good or bad. Git picks the next midpoint. After the last step it prints the first bad commit, with author, date and message. Then you end the session to return to where you started. Forgetting to end it is a classic way to find yourself on a detached head wondering what happened.

There is also a run mode. You give it a script, and it uses the script’s exit status: zero means good, a small set of non-zero values means bad, and one special value means “cannot test this commit, skip it”. With a script, the whole search runs unattended. That is the form I recommend whenever the check can be automated, because humans make mistakes at step seven of ten and the mistake sends the search to the wrong half without telling you.

What makes a bisect trustworthy

  • A deterministic check. If the bug appears one run in five, a single pass tells you nothing. A “good” verdict could be luck, and one wrong verdict poisons the result. Either make the test repeat enough times to be convincing, or find a more reliable symptom.
  • A known good start. Verify it. Many wasted afternoons begin with “it worked at the last release” when it did not.
  • Commits that build. If half the history has broken builds, skip those. Teams that keep each commit buildable and small make bisect nearly painless; teams with giant “WIP” commits get a giant suspect at the end.
  • The right thing under test. Database migrations, dependency versions and config files may sit outside what git checks out. If the bug depends on them, your test must set them up each time.

What the answer means

Bisect returns the first commit where the symptom appears, which is not always the commit that is wrong. A small change may merely expose a defect that had been sitting there. Read the diff before you blame. Similarly, with merge-heavy histories the first bad commit may be a merge, which tells you the problem is an interaction between two branches rather than a line anybody wrote. That is useful to know, and it is the kind of bug that is almost impossible to find by reading.

It also depends on the bug being monotonic: good up to some point, bad ever after. If a regression was introduced, then accidentally fixed, then reintroduced, the search can land on any of the transitions. When results look strange, run bisect again with a narrower range around the suspicious area.

The same trick outside git

The habit transfers to many places. A config file with 200 lines that makes the service crash: delete half, test, narrow. A failing test that only fails when the full suite runs: halve the set of tests that run before it. A deployment that went wrong somewhere among twelve changed services: roll half back. In every case you stop theorising and let a quick yes-or-no question cut the search space in half.

One practical point on culture: when you find the commit, describe it without blame. The point of the tool is a fast answer, and the most useful follow-up is a test that would have caught the change, added to the suite so the next regression is found by CI and not by you.

Leave a Reply

Trending