Finding bugs with bisect

Topic 1 of 4 in Power tools6 min readBeginnerFree course

Overview

“It worked last month.” Somewhere in the hundreds of commits since, something broke — but the change isn’t obvious. Reading every commit would take hours. git bisect finds the culprit by binary search: it repeatedly checks out the commit halfway between a known-good and a known-bad one, and you say whether the bug is there. Each answer halves the search.

git bisect: binary search for the bad commit. Nine commits c1 to c9: c1 is known good and c9 is known bad. Step 1, test c5: good. Step 2, test c7: bad. Step 3, test c6: bad, so c6 is the first bad commit. Commands: git bisect start; git bisect bad; git bisect good c1; and git bisect run npm test to let a script decide good or bad.

How fast is it?

With n commits to search, bisect needs about log₂(n) tests:

Commits between good and bad Tests needed
10 ~4
100 ~7
1,000 ~10
10,000 ~14

A manual bisect

Terminal
git bisect start
git bisect bad                 # the current commit has the bug
git bisect good v2.3.0         # this release didn't
Text
Bisecting: 47 revisions left to test after this (roughly 6 steps)
[4f1a2b3…] Update checkout styles

Git checks out a commit in the middle (in detached HEAD state). Test it — run the app, run a test, try the bug — and report:

Terminal
git bisect good       # the bug isn't here
git bisect bad        # the bug is here
git bisect skip       # can't test this one (e.g. it doesn't build)

Git checks out the next candidate. Repeat until:

Text
c6e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7 is the first bad commit
commit c6e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7
Author: …
    Refactor price helper

Read that commit’s diff — the bug is usually obvious once you’re looking at the right change. Then always end the session:

Terminal
git bisect reset      # back to the branch you started on

Useful while bisecting: git bisect log (your answers so far), git bisect visualize (remaining candidates), and git bisect terms --term-old fixed --term-new broken if “good/bad” reads backwards (for example when finding when a bug was fixed).

Automatic bisect with a script

If a command can tell good from bad, let Git do all the work:

Terminal
git bisect start
git bisect bad HEAD
git bisect good v2.3.0
git bisect run npm test

git bisect run runs the command at each step and uses its exit code:

Exit code Meaning
0 good
1–127 (except 125) bad
125 skip — this commit can’t be tested

Usually your whole test suite is too broad (other things may have been broken at times), so write a focused script that checks only the bug:

Terminal
#!/bin/sh
# test-price.sh — exit 0 if prices are right
[ "$(./price.sh)" = "30" ]

Keep the script outside the repository (or untracked), so checking out old commits can’t change or delete it. If old commits need building first, do it in the script and exit 125 if the build fails:

Terminal
#!/bin/sh
npm ci --silent || exit 125
npm test -- --grep "cart total"

Tips

  • Find a good commit quickly: try a release tag from a while ago, or check out something old with git switch --detach HEAD~200 and test it.
  • Keep commits buildable. Bisect relies on each commit working on its own — one reason Writing good commits asks for working commits. Lots of broken commits mean lots of skips.
  • Merge-heavy history: git bisect start --first-parent follows only the first parents (e.g. only merges into main), which can tell you which pull request introduced the bug.
  • Git 2.56 added a --reset-when-found option to git bisect, which runs git bisect reset for you once the culprit is found.

After you’ve found it

The commit tells you what changed; its message and pull request tell you why. Often the fix is to adjust that change rather than revert it entirely — but git revert <commit> is a quick safe option while you work on a proper fix. Add a test that would have caught the bug, so bisect isn’t needed for it again.

Remember bisect whenever you hear “it used to work”. It turns hours of guesswork into a few minutes of yes/no answers.

Exercise

Hunt down a regression automatically

The script builds a history of 30 commits. Somewhere in it, a commit broke the price calculation: price.sh should print 30 but now prints 10. The first commit is known to be good. Write a tiny test script and use git bisect run to find the first bad commit automatically, print its message, then end the bisect session and confirm you’re back on main.

Terminal
mkdir bisect && cd bisect && git init -qb main
printf '#!/bin/sh\necho $((10 * 3))\n' > price.sh && chmod +x price.sh
git add . && git commit -qm "Add price calculation"
for i in $(seq 1 28); do
  echo "change $i" >> changelog.txt && git add . && git commit -qm "Routine change $i"
  if [ "$i" = 17 ]; then
    printf '#!/bin/sh\necho $((10 * 1))\n' > price.sh && git commit -qam "Refactor price helper"
  fi
done
git log --oneline | wc -l          # 30 commits

# 1. Write a test that exits 0 when price.sh prints 30, non-zero otherwise

# 2. Bisect automatically

# 3. Show the culprit, then reset

Summary

  • git bisect binary-searches history to find the first commit where something broke.
  • Mark one bad and one good commit; Git checks out the middle, you test and answer good or bad.
  • It needs only about log2(n) steps — 1,000 commits take about 10 tests.
  • git bisect run <script> automates it: exit 0 = good, 1–127 = bad, 125 = skip this commit.
  • Always finish with git bisect reset to return to where you started.