Find the Commit That Broke Your App with Git Bisect
An AI coding session leaves behind several commits. The application still starts, but a customer now pays shipping on an order that should qualify for free delivery. Asking the agent to rewrite the calculation immediately may produce a plausible patch without explaining when the behavior changed.
A better first task is narrower: find the revision where a reproducible check changes from passing to failing. Git bisect helps locate that boundary. The agent can help express the check, inspect the resulting diff and propose a fix; the test supplies the evidence.
This guide is for developers investigating a regression in committed code. We ran the included synthetic shipping example locally on November 19, 2026, using Git 2.47.0 and Node.js 26.5.0. Those are observed versions, not minimum requirements. We did not test a real customer incident or compare coding agents.
What bisect does
Given a known passing revision and a known failing revision, Git tests the history between them by repeatedly narrowing the search. You classify each selected revision, or provide a command that does so. The Git book's debugging chapter explains this workflow and why it is useful when many commits separate working and broken behavior.
The useful output is a commit to investigate, not an explanation of intent or an automatic repair. Our recommendation is to freeze the reproduction before letting an agent modify the application.
A small bug you can reproduce
Our example treats amounts as integer cents. Shipping is free at a subtotal of 5,000 cents or more, and costs 500 cents below that threshold. The starting implementation is:
export function shippingCents(subtotalCents) {
return subtotalCents >= 5000 ? 0 : 500;
}The synthetic history contains an initial commit and seven subsequent revisions. Revision 4 replaces >= with >. Most order values behave as before; exactly 5,000 cents now returns 500 instead of zero. This is a deliberately small boundary bug, not a production pricing model.
Download the complete runnable demo. Inspect the script, save it locally, then run it with Bash:
bash /path/to/git-bisect-demo.shReplace the path with your saved file. The demo requires Git, Node.js and a Bash environment with mktemp. It creates a new temporary repository and linked investigation worktree, configures identity only in that repository, and leaves its files and log for inspection. It does not require an AI service, model credentials or a package installation.
In our run, the two endpoint checks returned 0 cents and 500 cents. The automated search then checked revisions 3, 5 and 4, identifying revision 4 as the first failing commit. The script also compares the discovered commit with the deliberately tagged introduction commit and stops if they differ. Your commit hashes will differ because the demo creates fresh history.
Keep the check outside the changing history
The demo writes this checker outside both working directories. It imports the implementation from the current working directory, so each invocation checks the revision Git has selected:
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
const source = resolve('src/shipping.mjs');
if (!existsSync(source)) process.exit(125);
try {
const { shippingCents } = await import(pathToFileURL(source).href);
const actual = shippingCents(5000);
console.log(`shippingCents(5000) = ${actual}`);
process.exit(actual === 0 ? 0 : 1);
} catch (error) {
console.error(error);
process.exit(128);
}For this investigation, a missing file is untestable, and an import or runtime exception is a reason to stop and investigate. Neither is silently treated as the shipping regression. A different investigation may need a different classification.
The bisect manual defines the command contract: zero means good; 1–127, except 125, means bad; 125 skips a revision; other exit codes abort. In particular, a missing command can return 127 and be mistaken for a bad revision. Verify your runner before starting. Skipping commits near the boundary can leave several possible culprits.
Apply the pattern to your repository
Use a separate, disposable worktree so the investigation can change revisions without disturbing the files you are editing. Worktrees share repository storage and refs; they are not a security sandbox. Git's worktree documentation documents detached checkouts for this sort of temporary work.
Here is the command pattern used by the demo. In your repository, replace demo-good and demo-bad with revisions you have actually checked, and replace the checker path with an absolute path to your own reproduction:
git worktree add --detach ../regression-investigation demo-bad
cd ../regression-investigation
git checkout demo-good
node /absolute/path/check-shipping.mjs
# Verify: exit 0 and the expected behavior.
git checkout demo-bad
node /absolute/path/check-shipping.mjs
# Verify: exit 1 for this exact regression.
git bisect start demo-bad demo-good --
git bisect run node /absolute/path/check-shipping.mjs
git bisect log > ../shipping-bisect.log
git show refs/bisect/bad -- src/shipping.mjs
git bisect resetThis is an interactive sequence: do not continue if the endpoint checks disagree with your assumptions. Save the log and inspect the diff before resetting. The final command ends the bisect session and returns to its starting checkout. These commands and the external-checker pattern follow the official bisect examples .
For a real application, make the runner build and test the checked-out revision using its own lockfile and compatible runtime. Keep generated files and services from one revision from contaminating the next. An installation outage or unrelated build failure needs diagnosis; it is not evidence about your target bug.
Give the agent a bounded investigation
A useful task description is:
Investigate why shipping at 5,000 cents changed from zero to 500. First verify the two endpoints with the same external check. Use the disposable worktree for bisection. Do not change application files or the check during the search. Return the first failing commit, its relevant diff, the bisect log and any untestable revisions. Propose a fix only after explaining the evidence.
This is a suggested instruction, not a claim about a particular agent's built-in behavior. After the investigation, create a normal fix branch from the current development baseline. Add a permanent regression test and review nearby cases: 4,999, 5,000 and 5,001 cents. A historical culprit identifies what to understand; it does not mean the whole old commit should be reverted without review.
When another approach is simpler
If the change is still uncommitted, inspect the working diff and run a focused test first. If there are only two small candidate commits, reviewing them directly may be enough. Bisect becomes useful when the history is wider and the symptom is repeatable.
A flaky test, mutable external service or bug that disappears and later returns can invalidate a simple passing-to-failing search. Stabilize the reproduction or narrow the interval before trusting the result. Squashed history also limits the answer: Git can locate the committed boundary, not reconstruct intermediate agent edits that were never committed.
Our demo uses only local processes and makes no model calls. If an agent assists with a real investigation, its usage charges depend on your service and plan; this guide makes no cost estimate. The main operational requirement is a check you trust enough to run unchanged across history.



