A bug that appears after several weeks of development can be difficult to trace. The current code may contain hundreds of commits since the last known-good release, and reading every diff is slow. Git bisect solves this problem by using binary search across commit history. You mark one revision as bad and another as good; Git checks out a commit between them and asks you to test it. Repeating the process narrows the range until the first bad commit is identified.
Git bisect is especially useful for regressions: behavior that worked at one point in history and later stopped working. It is less useful when the bug has always existed, when the test cannot be reproduced on historical commits or when the result changes nondeterministically. This guide shows how to define reliable boundaries, automate the search and avoid common mistakes.
How Git bisect works
Binary search repeatedly divides a range into smaller parts. If there are 128 candidate commits and each test reliably classifies a revision, the search needs roughly seven decisions to isolate one commit because each decision removes about half the remaining range. The exact number depends on the history graph and skipped revisions, but the principle is the same: test the middle rather than inspecting every commit in order.
Git does not know what your application considers correct. You supply the test by marking a commit good or bad. A good revision is one where the bug is absent; a bad revision is one where it is present. If either boundary is wrong, the search can return a misleading result. Spend time establishing reliable boundaries before starting.
Prepare a reproducible test first
Before running bisect, write down the exact failure. Include the input, environment, expected result, actual result and steps to reproduce. If the bug occurs only with a particular database version, browser, feature flag or data shape, preserve those conditions. A test that depends on a live external service or changing production data may classify old commits inconsistently.
Prefer a small automated test that exercises the failing behavior. It should return success only when the bug is absent and failure when the bug is present. If the application cannot be built at some historical commits, decide in advance how those revisions will be marked as untestable.
Start an interactive bisect session
First make sure your worktree is clean or safely stashed. Bisect checks out older commits, so uncommitted changes can complicate the process. Identify a known-good commit, such as the last release where the feature worked, and a known-bad commit where the regression is reproducible.
git status
git log --oneline --decorate -n 20
git bisect start
git bisect bad HEAD
git bisect good v2.4.0
The example assumes the current HEAD is bad and the v2.4.0 tag is known to be good. Replace those references with the revisions that match your repository. Git will check out a candidate commit. Run the reproduction test and mark the result:
git bisect bad
# or
git bisect good
Continue until Git reports the first bad commit. Read the commit message and diff, then inspect the surrounding code and tests. The first bad commit is a strong lead, but it is not automatically proof that one line in that commit is the sole cause. A change may expose an existing race, alter timing or depend on an earlier incompatible change.
Automate the search with git bisect run
When a deterministic test can classify every candidate commit, git bisect run can execute it automatically. Create a script that returns zero when the revision is good and a non-zero status when it is bad. Git interprets exit status 125 as skip, which is useful when a commit cannot be tested because a dependency or build prerequisite is unavailable.
#!/usr/bin/env bash
set -u
# Keep this script outside the repository so it survives old checkouts.
if [ ! -f composer.json ] || [ ! -f tests/RegressionTest.php ]; then
exit 125
fi
if ! composer install --no-interaction --prefer-dist; then
exit 125
fi
if [ ! -x ./vendor/bin/phpunit ]; then
exit 125
fi
./vendor/bin/phpunit tests/RegressionTest.php
status=$?
if [ "$status" -eq 0 ]; then
exit 0
fi
exit 1
Save the script outside the repository, for example as ~/bisect-test.sh, so Git can still execute it after checking out an older commit that does not contain the script. The example uses Composer and PHPUnit; substitute the dependency installation and test runner for your project. If the target test file or its prerequisites do not exist at a historical revision, return 125 because the result is unknown. Do not return success when the test did not actually run.
chmod +x ~/bisect-test.sh
git bisect start
git bisect bad HEAD
git bisect good v2.4.0
git bisect run ~/bisect-test.sh
Git will check out candidate revisions, run the script and use its exit status to choose the next half of history. Exit status zero means good. Exit status 125 means skip. Other non-zero statuses in the documented range are treated as bad; make sure your script does not accidentally return a special status for an infrastructure failure that should have been skipped.
Handle commits that cannot be tested
Some commits do not build, require a dependency that is no longer available or do not contain the test harness. Use git bisect skip for a revision that cannot be classified. You can skip a single commit or a range, but too many skipped revisions reduce the precision of the result. If the search cannot distinguish the first bad commit because all candidates in a region were skipped, Git may report a range of possible commits rather than one answer.
When a revision is untestable, record why. A skipped commit because the project does not compile is different from a skipped commit because an external test service is unavailable. The distinction matters when someone reviews the result later.
Use a custom test harness for flaky or complex regressions
Flaky tests are a major threat to bisect accuracy. A test that fails one time in ten may label a good commit as bad and send the search down the wrong branch. Repeat the test where possible, isolate external dependencies, seed random data and use a stable environment. For timing-sensitive bugs, collect enough observations to distinguish a real regression from normal variance.
If the bug depends on a specific database schema or dataset, keep a versioned fixture. If it depends on a compiler or runtime, use a container or toolchain manager that can reproduce the relevant version. A good bisect script should be boring: build, run one targeted test, return a clear exit status and clean up its temporary resources.
Understand merge commits and path-limited searches
Repositories with frequent merges can contain several possible paths through history. Git bisect normally reasons about the commit graph, which is usually what you want. In some integration workflows, a first-parent search can help focus on the sequence of commits that landed on the main branch. Use it only when that is the question you intend to answer; it can skip commits on merged branches that may contain the actual change.
Git also supports limiting a bisect search to paths. That can be useful when the regression is clearly isolated to one directory, but it can also exclude a related configuration or dependency change. Start with the full history unless you have a defensible reason to narrow the search.
Inspect and preserve the result
When Git identifies a candidate, inspect the commit and its parents:
git show --stat --oneline <commit>
git show --format=fuller <commit>
git bisect log
The bisect log records the good, bad and skipped decisions. Save it with the bug report if the investigation needs to be reproduced. If you need to repeat the same search later, the log can be replayed in a new bisect session with git bisect replay.
After the investigation, return to the branch you were working on:
git bisect reset
git status
Do not forget the reset step. Until you reset, the worktree may remain checked out at an older commit. If you need to preserve the identified revision for further work, create a separate branch after recording the result.
Common Git bisect mistakes
- Choosing an uncertain good boundary: verify the bug is absent with the same reproduction steps.
- Testing a different environment at each revision: keep dependencies, runtime and test data stable.
- Using a flaky test: stabilize it or repeat measurements before trusting the classification.
- Marking an unbuildable commit as bad: use skip when the result is unknown.
- Changing files during the search: keep the worktree clean and store test scripts outside tracked source or in a separate stable location.
- Stopping without reading the diff: inspect the commit and surrounding history before assigning cause.
- Forgetting git bisect reset: return to the original branch after the search.
When Git bisect is not the right tool
Bisect is designed for a behavior that changes from good to bad somewhere in a history range. It is not a substitute for profiling a performance problem that fluctuates continuously, diagnosing a production-only data corruption event without a reproducible test or finding a bug that existed before the oldest known-good commit. It can also be difficult when the test requires unavailable external infrastructure or when the project has large, incompatible historical changes.
In those cases, combine other techniques: inspect release notes, compare configuration changes, use logs and traces, add a targeted regression test or reproduce the issue in a minimal environment. Bisect works best when it has a precise question and a reliable yes-or-no answer.
Use bisect as part of a stronger engineering workflow
Teams can make future investigations faster by keeping regression tests small, preserving build instructions, pinning toolchains and recording the last known-good release for important features. Code review should include tests for behavior changes, and deployment pipelines should retain enough metadata to connect a release to the source revision and build environment.
For adjacent practices, see FutureTechie’s code review checklist for AI-generated changes, Git security update coverage and GitHub Actions OIDC guide. Those articles cover review and pipeline controls that help prevent regressions from reaching production.
Official Git references
Bottom line: git bisect turns a long regression hunt into a repeatable binary search. Its accuracy depends on trustworthy good and bad boundaries, a deterministic test and disciplined handling of skipped commits. Automate the test when possible, preserve the bisect log and reset the worktree when finished. Used well, it can turn a vague report that something broke last week into a focused commit for code review.





