When a test or behavior worked in one revision and fails in another, git bisect can narrow a long history to the commit that introduced the regression. This checklist explains the complete workflow, including repeatable test scripts, dependency-aware builds, skipped revisions, nondeterministic failures, and safe recovery steps.
Overview
Git bisect uses a binary-search process. You identify one known-good commit and one known-bad commit, then Git checks out a midpoint between them. You test that revision and mark it good or bad. Git repeats the process until the remaining range identifies the first commit where the behavior changed.
The method is effective only when the result is repeatable and the good and bad boundaries are accurate. Before starting, write down:
- The exact symptom, failing test, endpoint response, crash, or performance threshold.
- A known-bad revision where the problem is present.
- A known-good revision where the problem is absent.
- The command or script that can classify a checked-out revision.
- Any setup steps required for dependencies, generated files, firmware, databases, or services.
Save uncommitted work before bisecting. A bisect changes the working tree repeatedly, so commit, stash, or otherwise preserve local changes first. If the project has build instructions, use the same configuration for every revision. For example, a CMake project may require a consistent compiler, build directory, and test target; the project structure guidance in the CMake project structure guide can help make that setup repeatable.
Basic command workflow
git status
git bisect start
git bisect bad <known-bad-commit>
git bisect good <known-good-commit>
# test the checked-out revision
git bisect good
git bisect bad
git bisect reset
You can use branch names, tags, or commit IDs for the boundaries. After each classification, inspect the output from Git and follow the next checked-out revision. When the search completes, Git reports a candidate commit. Read that commit with git show, inspect its parent, and rerun the regression test on both sides before treating it as confirmed.
Checklist by scenario
Scenario 1: Manual investigation
- Start from a clean worktree and record the current branch with
git branch --show-current. - Choose a bad revision that reproduces the issue and a good revision that does not.
- Run
git bisect start, then mark the boundaries withgit bisect badandgit bisect good. - At each midpoint, build or prepare the project using the same procedure.
- Run the smallest reliable reproduction. Mark the revision good only when the failure is clearly absent; otherwise mark it bad.
- When Git identifies a candidate, verify the result against its parent and record the evidence in the issue or change log.
- Finish with
git bisect resetto return to the original checkout.
Scenario 2: Automated git bisect run
Automation is useful when the test has a clear exit status. The convention is important: exit code 0 means good, exit code 1 through 124 means bad, and exit code 125 tells Git to skip a revision that cannot be tested.
Create a repository-local script such as scripts/bisect-regression.sh:
#!/usr/bin/env bash
set -u
if ./scripts/reproduce-regression.sh; then
exit 0
else
exit 1
fi
Make it executable, verify it directly on a known-good and known-bad revision, then run:
chmod +x scripts/bisect-regression.sh
git bisect start
git bisect bad <known-bad-commit>
git bisect good <known-good-commit>
git bisect run ./scripts/bisect-regression.sh
git bisect reset
The script should prepare only what each revision needs and should not silently reuse stale output. Remove or isolate generated artifacts when they can affect the result. A test that passes because an old binary, cache, database, or dependency remains in place can produce a convincing but incorrect answer.
Scenario 3: Dependency-aware builds
Some historical revisions do not compile with today’s dependency set, or they require a different build step. Do not automatically classify every build failure as a regression. If the revision is not testable, return 125:
#!/usr/bin/env bash
set -u
rm -rf build
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo || exit 125
cmake --build build --parallel || exit 125
if ctest --test-dir build --output-on-failure -R regression_case; then
exit 0
else
exit 1
fi
Use this pattern only when a build failure means “cannot evaluate this revision,” not when the build failure itself is the defect being investigated. For dependency changes, pin versions where practical, use the project’s lockfile or documented environment, and record which revisions were skipped.
Decision table
| Observation | Classification | Action |
|---|---|---|
| Reproduction is absent and the test completes normally | Good | Run git bisect good or return exit code 0 |
| Reproduction is present and the test completes normally | Bad | Run git bisect bad or return exit code 1 |
| Dependencies or source prevent a meaningful test | Skipped | Run git bisect skip or return exit code 125 |
| Result changes between repeated runs | Unknown | Stabilize the environment before classifying |
What to double-check
- Boundary validity: Reproduce the issue at the bad boundary and confirm the good boundary with the same command. If either boundary is wrong, the search result is not meaningful.
- Test scope: Test the behavior introduced by the suspected regression, not merely a related symptom. For an API problem, preserve request method, payload, headers, authentication state, and service configuration; a consistent workflow such as the REST API debugging guide can help.
- Environment: Keep operating system, compiler, runtime, feature flags, locale, timezone, hardware, and service versions stable. Embedded failures may require the same board, firmware settings, probe, and interrupt conditions; use the embedded C debugging checklist when the reproduction involves a device.
- Generated state: Clear build directories, caches, temporary databases, and generated assets when they influence the result.
- Commit meaning: The first bad commit may expose a problem introduced earlier, depend on an incomplete change, or alter a test without changing production behavior. Review the diff, parent, related files, and test changes.
- Skipped commits: A large number of skips weakens the result. Narrow the boundaries, repair the historical build, or run a second bisect with an environment that can evaluate more revisions.
Common mistakes
Marking build failures as bad. A compiler error can mean that a revision is incompatible with the current toolchain rather than that it introduced the runtime regression. Decide in advance whether build failure is a valid failure signal.
Using a flaky test. Git assumes each classification is reliable. For a nondeterministic test, run it several times and classify only after applying a defined rule, such as requiring all repetitions to agree. Better still, remove timing dependence, isolate external services, or replace the symptom check with a deterministic assertion.
Leaving local changes in place. Local files can make old commits appear fixed or broken. Use a clean checkout and verify it with git status before starting.
Forgetting to reset. Bisect leaves the repository at the last tested revision. Always run git bisect reset when finished, then restore your branch and local work carefully.
Automating setup incorrectly. A script that downloads moving dependencies, contacts an unstable service, or reuses stale artifacts is not a repeatable test. Pin inputs where possible and print enough diagnostic output to explain a failed classification.
Assuming the first bad commit is the complete root cause. It is the first commit in the tested range that changes the observed result. Follow-up changes, missing dependencies, or an earlier latent defect may still matter.
When to revisit
Keep this workflow available whenever a release shows a new regression, a long-lived branch diverges from a stable branch, or a previously passing test begins failing after several merges. Revisit the test script when build commands, package managers, compilers, CI images, firmware toolchains, or service fixtures change. A script that worked for one investigation can become misleading if its setup silently changes.
Before a planned maintenance or seasonal planning cycle, confirm that the repository’s reproduction command still works from a clean checkout. Run it against a known-good revision and a deliberately failing fixture if the project provides one. Review exit-code handling, dependency pinning, cleanup behavior, and CI permissions.
For the next regression, use this short action list:
- Preserve local work and create a minimal, repeatable reproduction.
- Prove the good and bad boundaries.
- Choose manual bisecting for nuanced results and
git bisect runfor deterministic tests. - Use exit code
125for genuinely untestable revisions, not for uncertain results. - Verify the candidate against its parent, document skipped commits and environment details, and reset the repository when done.
Used this way, Git bisect is not a one-off command but a reusable developer productivity practice: it turns a broad historical search into a controlled experiment that can be repeated when the codebase, toolchain, or regression changes.