When something worked last week and is broken today, git bisect finds the exact commit that introduced the regression without you reading every diff in between. You tell Git one commit that is known good and one that is known bad, and it checks out a commit halfway between them. You test it, tell Git whether it is good or bad, and it halves the range again. After a handful of tests it reports <sha> is the first bad commit. Over a range of a thousand commits, that is about ten tests instead of a thousand. This guide covers the manual loop, the automated git bisect run script, skipping untestable commits, reading the result, and cleaning up with git bisect reset.
What you’ll accomplish
- Start a bisect session with a known-good and a known-bad reference.
- Narrow the range by marking each checkout
goodorbaduntil Git names the first bad commit. - Automate the whole search with
git bisect run <script>and its exit-code contract. - Skip commits that cannot be built or tested with
git bisect skip. - Confirm the result with
git showand return to your original branch withgit bisect reset.
Prerequisites
- Git 2.x on your machine (
git --version). Bisect has shipped with Git for years; nothing extra to install. - A reproducible test for the bug. It can be a manual check (“open the page, the total is wrong”) or, better, a script or command that fails when the bug is present and passes when it is not. A repeatable test is what makes bisect trustworthy.
- A known-good ref and a known-bad ref. The bad one is usually your current tip (
HEAD). The good one is any earlier commit, tag, or release you are sure worked — an older tag likev2.3.0, a commit SHA, orHEAD~200. - A clean working tree. Commit or stash your changes first, because bisect checks out different commits and will refuse to move over uncommitted edits.
How it works
git bisect runs a binary search over the commits between good and bad. Git assumes the bug was introduced by one commit and is present in every commit after it, so the history splits cleanly into a “good” prefix and a “bad” suffix. Each test tells Git which half the boundary is in, so it can throw away the other half. Halving a range of N commits takes about log2(N) steps: roughly 10 tests for 1,000 commits, 13 for 8,000.
You can drive that loop by hand — Git checks out a commit, you test it, you type git bisect good or git bisect bad — or you can hand Git a script and let it run the whole search unattended with git bisect run. The automated mode communicates through the script’s exit code, and the contract is specific:
- 0 means the commit is good.
- Any code from 1 to 127 except 125 means the commit is bad. In practice a failing test suite exits
1;126(not executable) and127(command not found) also count as bad, which is worth remembering because a broken script path will be read as “bad”, not as an error. - 125 means the commit cannot be tested — treat it like
git bisect skip. Use this when the code does not build for reasons unrelated to your bug. - Codes 128 and above abort the bisect entirely (Git itself uses these for fatal errors).
Step-by-step
1. Start the session. From inside the repository, tell Git you are beginning a bisect. Nothing visible changes yet — Git is now recording state under .git/.
git bisect start
status: waiting for both good and bad commits
2. Mark the current commit as bad. Your tip has the bug, so mark it bad. With no argument, git bisect bad uses HEAD.
git bisect bad
status: waiting for good commit(s), bad commit known
3. Mark a known-good commit. Point Git at a commit, tag, or release you are certain predates the bug. As soon as Git has both ends, it checks out the midpoint and tells you how much range is left.
git bisect good v2.3.0
Bisecting: 214 revisions left to test after this (roughly 8 steps)
[9c2f1a7e6b5d4c3a2f1e0d9c8b7a6f5e4d3c2b1a] Cache invalidation on write
4. Test the checked-out commit and report the result. Git has now checked out that midpoint commit in your working tree. Run your test — the suite, the command, or the manual check — then tell Git the verdict. If the bug is present, mark it bad; if not, mark it good. Either way Git jumps to the next midpoint.
git bisect bad
Bisecting: 106 revisions left to test after this (roughly 7 steps)
[4b7d9e2a1c8f5b3e6d0a9c2f7b4e1d8a5c3f0b6e] Switch session store to Redis
5. Keep going until Git names the commit. Repeat step 4 — test, then git bisect good or git bisect bad — for each checkout. The “revisions left” number falls by about half each time. When only one candidate remains, Git prints the result.
git bisect good
5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e is the first bad commit
commit 5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e
Author: Dev <dev@example.com>
Date: Tue Jul 8 11:42:19 2026 -0400
Round tax to integer cents before summing
src/billing/total.js | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
6. Skip a commit you cannot test. If a checkout does not build for an unrelated reason, or the test cannot run there, do not guess. Tell Git to set it aside and try a nearby commit instead.
git bisect skip
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[a1c8f5b3e6d0a9c2f7b4e1d8a5c3f0b6e4b7d9e2] Rename config loader
7. Automate the search with a script. Instead of testing by hand, hand Git a command that exits 0 when the commit is good and non-zero when it is bad. Git runs it at each step and drives the whole search to the end. Start fresh, set the two ends, then hand off with git bisect run.
git bisect start
git bisect bad
git bisect good v2.3.0
git bisect run npm test --silent
running 'npm test' '--silent'
Bisecting: 106 revisions left to test after this (roughly 7 steps)
running 'npm test' '--silent'
Bisecting: 52 revisions left to test after this (roughly 6 steps)
running 'npm test' '--silent'
...
5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e is the first bad commit
bisect run success
8. Use a wrapper script when the test needs setup. If a bare command is not enough — you need to build first, or exit 125 for commits that will not compile — write a small script that returns the right codes, mark it executable, and pass it to git bisect run.
cat > bisect-test.sh <<'EOF'
#!/usr/bin/env bash
# 125 = can't test (build failed); 0 = good; 1 = bad
make build || exit 125
./run-check.sh
EOF
chmod +x bisect-test.sh
git bisect run ./bisect-test.sh
running './bisect-test.sh'
Bisecting: 26 revisions left to test after this (roughly 5 steps)
...
5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e is the first bad commit
bisect run success
9. Reset when you are done. Bisect leaves you on a detached checkout of some midpoint commit. git bisect reset ends the session and returns you to the branch and commit you started from.
git bisect reset
Previous HEAD position was 5f0a9c2 Round tax to integer cents before summing
Switched to branch 'main'
Validate it works
Confirm the commit Git named actually contains the change that caused the bug. Read its full diff — the culprit is usually obvious once you are looking at the right three lines.
git show 5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e
commit 5f0a9c2f7b4e1d8a5c3f0b6e4b7d9e2a1c8f5b3e
Round tax to integer cents before summing
- const tax = subtotal * rate;
+ const tax = Math.round(subtotal * rate);
For a second opinion, verify the boundary by hand: check out the named commit and the one right before it, running your test on each. The parent should pass, the named commit should fail.
git checkout 5f0a9c2^ # the parent — should be good
# run your test: passes
git checkout 5f0a9c2 # the named commit — should be bad
# run your test: fails
git switch - # back to where you were
If the parent passes and the named commit fails, the result holds. You can now write a targeted fix, or git revert 5f0a9c2 to back the change out while you work on a proper correction.
Troubleshooting
A flaky test gives you a wrong answer. If the test sometimes passes and sometimes fails on the same commit, bisect will follow the noise and blame the wrong commit. Run the test a few times on a single checkout first. If it is genuinely flaky, mark ambiguous checkouts with git bisect skip rather than guessing, and make the test deterministic before trusting the result.
The build breaks for an unrelated reason. Some old commit in the range fails to compile because of a dependency or tooling issue that has nothing to do with your bug. Do not mark it good or bad — mark it git bisect skip, or in a script exit 125. Git works around skipped commits and only reports uncertainty if the culprit hides entirely inside a skipped stretch.
You forgot to reset and are stuck on a detached HEAD. After a session you may be sitting on a random midpoint commit in detached HEAD. That is normal — nothing is lost. Run git bisect reset to return to your original branch. To land on a specific commit instead, use git bisect reset <sha>.
The first bad commit is wrong because good and bad were swapped. If you marked the buggy end “good” and the working end “bad”, Git searches the wrong direction and names nonsense — often it reports the merge-base or a very old commit. Run git bisect log to review your marks, git bisect reset, and start over with the ends the right way round: bad is where the bug is present.
Bisect refuses to run because the tree is dirty. If you have uncommitted changes, Git will not check out other commits over them. Commit the work on a scratch branch or git stash it, run the bisect, then restore. A build artifact that is not ignored can cause the same refusal — add it to .gitignore or clean it first.
When this is the wrong approach
Bisect is the fastest route to a culprit commit when history is roughly linear and the bug is reproducible. It is a poor fit when:
- You have no reliable repro. Bisect is only as good as your test. If you cannot tell good from bad consistently, every step is a coin flip and the result is meaningless. Build a deterministic repro first, even a crude one.
- The history is a tangle of large merges. Bisect handles merges, but a range dominated by huge, interleaved feature merges can land you on commits that never built on their own, forcing many skips and a fuzzy answer. Bisecting a single branch’s first-parent history, or the range since the last known-good release, is cleaner.
- The change is not a single commit. If the regression emerges from an interaction across several commits, or from a config or data change outside the repo, the “first bad commit” can point you astray. Look at the boundary Git finds as a strong hint, not a verdict.
For undoing or recovering around a bad change once you have found it, see recover a deleted Git branch or lost commit with git reflog. The authoritative reference is the official git bisect documentation.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.