Git Bisect: Find the First Bad Commit with a Binary Search
Use git bisect to binary-search history between a good and bad commit, automate the midpoint test, and verify the first bad commit before fixing it.
08 Oct 2025, 20:52 UTC

The useful answer
Use git bisect when a regression exists somewhere between a known-good commit and a known-bad commit, and you want the first commit that introduced it. Instead of reading every diff, Git checks out midpoints, you mark each midpoint good or bad, and the remaining range is halved each step. For N commits in the range, expect roughly log2(N) tests, assuming the bug is deterministic and the test is reliable.
Bisect does not prove why the bug happened. It narrows the search to one commit, which you then inspect with git show, tests, and code review. Treat it as a locator, not a root-cause analysis.
How the mechanism works
You give Git two endpoints: a commit where the behavior is good and a commit where it is bad. Git assumes the regression is monotonic, meaning once the bug appears it remains present in later commits. It checks out a midpoint, runs your test or asks you to judge it, records the result, and repeats until only one candidate remains.
During the session Git uses a detached HEAD and stores state under .git, including BISECT_LOG. That is why the session must be ended with git bisect reset; otherwise later checkouts can be confusing.
Worked example: automate the midpoint test
Run this in a local clone with a clean working tree. You need write access to the clone, an executable test script, and enough disk and dependency cache for repeated builds. Do not run it on a dirty tree with unrelated changes, and prefer a disposable clone if the build writes large artifacts. The refs below are placeholders: replace v1.4.0 with a known-good tag or SHA and keep HEAD as bad only if the current checkout is truly bad.
git status --short
# expect no output; commit or stash unrelated work first
git bisect start
git bisect bad HEAD
git bisect good v1.4.0
git bisect run ./scripts/repro-checkout.sh
# when Git reports the first bad commit:
git show --stat FIRST_BAD_SHA
git bisect resetThe script is the important part. It must exit 0 when the commit is good, nonzero when the commit is bad, and 125 when the commit cannot be tested and should be skipped. Check man git-bisect on your installed Git for the exact exit-code contract and supported options; behavior is stable across modern Git, but local help is the source of truth.
#!/usr/bin/env bash
set -uo pipefail
./gradlew :app:test --tests 'com.example.CheckoutTest' >/tmp/bisect-checkout.log 2>&1
status=$?
if [ $status -eq 0 ]; then
exit 0
fi
if grep -qE 'Compilation failed|Could not resolve' /tmp/bisect-checkout.log; then
exit 125
fi
exit 1This is a template, not a tested run in your repository. Replace the Gradle command with the smallest reliable reproduction you have: a unit test, an integration test, a CLI invocation, or a script that checks a rendered page. Avoid using the full test suite if a targeted check is enough, because bisect multiplies the cost of each test by about log2(N).
Expected checks: git bisect log should show the good and bad endpoints and each tested midpoint. .git/BISECT_LOG should contain the same trail. The final message should identify one first bad commit. After git bisect reset, git status should show you back on the branch you started from, with bisect state cleared.
Verify the result before you trust it
A bisect result is only as good as the endpoints and the test. Before opening a fix, confirm the boundary manually. This changes checkouts, so end with git bisect reset or return to your branch afterward.
git checkout FIRST_BAD_SHA^
./scripts/repro-checkout.sh # expect pass
git checkout FIRST_BAD_SHA
./scripts/repro-checkout.sh # expect fail
git checkout main
# or: git bisect reset, if a bisect session is still activeIf the parent passes and the named commit fails, the bisect did its job. If both pass, your bad endpoint or test is wrong. If both fail, your good endpoint is wrong or the bug is not monotonic.
Limits and common mistakes
| Situation | What happens | Practical response |
|---|---|---|
| Flaky test | A midpoint can be marked good or bad by chance | Stabilize the test, repeat it, or use a deterministic assertion |
| Commit does not build | The binary search cannot judge that midpoint | Use git bisect skip or exit 125 from git bisect run |
| Too many skips | Git may report a range instead of one commit | Fix the build for skipped commits or narrow endpoints |
| Non-monotonic bug | The first bad commit may be misleading | Use a different oracle or investigate environment and data |
| Rewritten history or merges | The candidate may be a merge or a commit with mixed changes | Inspect with git show --stat and git log --oneline --graph |
- Do not mark a flaky result as good or bad. Re-run it or make the check deterministic first.
- Do not forget
git bisect reset. Leaving bisect active can make later checkouts look haunted. - Do not use an unrelated failing test as the oracle. The test must fail because of the regression you care about.
- Do not assume the first bad commit contains the faulty line. It may only be where the failing behavior became observable.
- Do not bisect across an environment change unless that is the variable you intend to isolate.
When not to use bisect
Bisect is a poor fit for intermittent bugs, heisenbugs that depend on timing, regressions caused by external services, and changes that cannot be built at old commits. It is also weak when the good endpoint is not actually good or the bad endpoint fails for a different reason. In those cases, first build a reliable reproduction, pin the environment, and only then bisect. If the repository cannot compile historical commits, consider bisecting a smaller artifact, such as a dependency version range or a generated file, rather than the whole tree.
The practical rule is simple: if you can name a good commit, name a bad commit, and write a script whose exit code reflects the regression, bisect is one of the fastest ways to find the introducing change. If you cannot do those three things yet, fix the oracle before you fix the bug.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.