Find the Bad Commit Introducing a Regression with Git Bisect
Locate the regression commit quickly with Git bisect – binary‑search the commit history, automate testing, handle limitations, and verify the result.
20 Nov 2026, 10:07 UTC

The problem: a test fails after a recent change
When a test suite suddenly reports a failure, you need to discover which commit introduced the regression. Manually checking dozens or hundreds of commits is inefficient, and guessing wastes time.
Git bisect – a binary‑search tool
Git bisect performs a binary search across the commit graph. You provide a known good commit and a known bad commit; Git checks out intermediate revisions, you run a test, and Git narrows the range until it reports the first bad commit.
Prerequisites
You need read access to the repository and write access to the working tree. No administrative privileges are required. The bug must be reproducible and the test must be deterministic. Git version 1.5 or newer is required for `git bisect`; `git bisect run` depends on the script you supply.
Manual workflow
Start the session and mark the boundaries. Replace
v1.0.0with a known good reference andHEADwith the known bad reference:git bisect start git bisect bad HEAD git bisect good v1.0.0Git checks out a commit roughly halfway between the two endpoints. Run your test or reproduce the bug manually.
Mark the outcome:
git bisect good # test passes, commit is good # or git bisect bad # test fails, commit is badRepeat the previous step until Git announces the first bad commit, showing its hash and subject line.
Conclude the session and return to your original branch:
git bisect reset
git bisect reset is essential because bisect leaves the repository in a detached HEAD state. After resetting, normal checkouts and merges work as expected. The bisect state is recorded in .git/BISECT_LOG.
Automating the test
If you have a command that exits with status 0 when the bug is absent and a non‑zero status when it is present, you can let Git drive the bisect. Create a script, for example check-regression.sh:
#!/bin/sh
# Placeholder: replace with your actual test runner
./run-failing-test.sh
Make the script executable and run the bisect:
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
git bisect run ./check-regression.sh
git bisect reset
Git checks out each candidate commit, executes the script, and uses the exit status to label the commit good or bad. The outcome depends entirely on the script’s correctness; Git does not validate the test logic.
Limitations and verification
- Reproducibility: Bisect assumes the test reliably distinguishes good from bad. Flaky or nondeterministic tests can mislead the search.
- Merge commits: In histories with many merges, Git may check out a merge commit that never existed as a linear series, complicating root‑cause analysis.
- Performance: Each bisect step runs your test; slow tests increase the total time required.
To verify that the identified commit truly introduced the regression, reproduce the failure at the commit hash reported by bisect and confirm that it is absent at the good commit. After finishing, run:
git bisect reset
git status
git log --oneline -1
git status should show you back on your original branch with no active bisect state, and git log should display the commit you were on before starting.
Closing
When a regression spans a long history, Git bisect offers a fast, systematic way to pinpoint the offending commit. Use it when you have reliable good and bad references and a repeatable test; automate the test with `git bisect run` for large ranges. Always finish with `git bisect reset` to restore a normal repository state.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.