Finding bugs with bisect
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.
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
git bisect start
git bisect bad # the current commit has the bug
git bisect good v2.3.0 # this release didn'tBisecting: 47 revisions left to test after this (roughly 6 steps)
[4f1a2b3…] Update checkout stylesGit checks out a commit in the middle (in detached HEAD state). Test it — run the app, run a test, try the bug — and report:
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:
c6e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7 is the first bad commit
commit c6e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7
Author: …
Refactor price helperRead that commit’s diff — the bug is usually obvious once you’re looking at the right change. Then always end the session:
git bisect reset # back to the branch you started onUseful 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:
git bisect start
git bisect bad HEAD
git bisect good v2.3.0
git bisect run npm testgit 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:
#!/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:
#!/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~200and 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-parentfollows only the first parents (e.g. only merges intomain), which can tell you which pull request introduced the bug. - Git 2.56 added a
--reset-when-foundoption togit bisect, which runsgit bisect resetfor 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.
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.
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 resetmkdir 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
# (kept outside the repository so checking out old commits can't change it)
cat > ../test-price.sh <<'EOF'
#!/bin/sh
[ "$(./price.sh)" = "30" ]
EOF
chmod +x ../test-price.sh
# 2. Bisect automatically
git bisect start
git bisect bad # the current commit is broken
git bisect good "$(git rev-list --max-parents=0 HEAD)" # the first commit worked
git bisect run ../test-price.sh # "… is the first bad commit"
# 3. Show the culprit, then reset
git log -1 --format="%h %s" refs/bisect/bad # Refactor price helper
git bisect reset
git branch --show-current # mainSummary
git bisectbinary-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 resetto return to where you started.