Debug AI-generated code with Git bisect
Your coding agent refactored checkout over several commits. A $50 cart now gets a $19 shipping quote instead of $12. Before asking the agent to fix checkout, use git bisect to find the first commit that returns $19 for that cart. Git checks out commits between a known working version and a broken one. You give it the same test at each step.
In a nine-commit demo, running a check that treated every process error as the bug pointed at an unrelated broken module. A check that returned 125 for that module found the commit that actually changed the quote. Both runs used the same history and endpoints.
Pick a good commit and a bad commit
Use two commits you can test. A release tag helps if you know the cart worked in that release. Use HEAD for the bad end only if the bug reproduces from committed files. If it only happens with the agent's uncommitted edits, inspect that diff first. Git bisect only searches commits.
Run these commands from your project in Bash, or Git Bash on Windows. Replace the example tag checkout-working with your known-good tag or commit ID. The ancestry check should print exit status 0. If it returns anything else, choose an earlier working commit on the bad revision's history before continuing. Git merge-base documentation
good=$(git rev-parse --verify 'checkout-working^{commit}')
bad=$(git rev-parse --verify 'HEAD^{commit}')
git merge-base --is-ancestor "$good" "$bad"
echo $?
Create a worktree in a sibling folder. Git will change the files in that checkout during the search, so you can keep your open editor and uncommitted edits in the original folder. Use a new folder name if ../checkout-bisect already exists. Git worktree documentation
git worktree add --detach ../checkout-bisect "$bad"
cd ../checkout-bisect
A separate worktree shares the repository's Git metadata and runs your project with your normal user permissions. Stop any dev server that would cause your checks to hit the original app by mistake. For an API or browser repro, use a dedicated port, local fixtures and the same runtime, configuration and dependency setup at each commit. If the project needs dependencies, use the lockfile of the commit under test.
Write a test for the exact regression
In this example, shipping.cjs exports a function that takes an order subtotal in cents and returns the shipping price in cents. A second cart at the $75 free-shipping threshold acts as a control. Adapt the inputs and expected outputs to the behavior you can reproduce.
Save the script as ../bisect-shipping.cjs from inside the bisect worktree. Keeping it outside the worktree means the agent's older edits to tests and fixtures won't change what the script measures. Run it with Node from the worktree root.
const path = require('node:path')
function skip(reason) {
console.error('SKIP: ' + reason)
process.exit(125)
}
let quote
let control
try {
const shipping = require(path.resolve('shipping.cjs'))
quote = shipping(5000)
control = shipping(7500)
} catch (error) {
skip(error.message)
}
if (control !== 0) {
skip('The $75 cart did not qualify for free shipping')
}
if (quote === 1200) {
console.log('GOOD: $50 cart shipped for $12')
process.exit(0)
}
if (quote === 1900) {
console.error('BAD: $50 cart shipped for $19')
process.exit(1)
}
skip('Unexpected quote for the $50 cart: ' + String(quote))
| Script result | Meaning for this search |
|---|---|
0 |
The $50 cart cost $12 to ship and free shipping still worked. |
1 |
The $50 cart cost $19 to ship and free shipping still worked. |
125 |
The module failed to load, the control failed, or the cart produced a different result. |
Git treats most nonzero exit codes below 128 as bad, including common shell errors. Keep the exit-code mapping in the wrapper if your app test fails for several reasons. An ordinary npm test can be enough when you are bisecting the suite itself and every tested revision can run it. For a checkout regression, a failed dependency install tells you that the revision is untestable for this question. If the same setup error occurs at both endpoints, fix the environment before starting the search. Git bisect exit codes
Check both ends before starting Git bisect
git switch --detach "$good"
node ../bisect-shipping.cjs
echo $?
git switch --detach "$bad"
node ../bisect-shipping.cjs
echo $?
You should see 0 for the good commit and 1 for the bad commit. If both return the same result, narrow the repro or choose different endpoints. If running the check again at the same commit changes the answer, pin the inputs, seed the data, or remove the network dependency before automating the search. If you still need to see the bug in a browser, run git bisect start "$bad" "$good" and use git bisect good, git bisect bad, or git bisect skip after each manual check.
Run the search and verify the first bad commit
git bisect start "$bad" "$good"
git bisect run node ../bisect-shipping.cjs
git bisect log
In the demo, commit 5 of 9 temporarily removed the shipping module and commit 6 restored it. Commit 7 changed the $50 quote from $12 to $19. An unhandled module-load error exited 1, so the naive check named commit 5 as the first bad commit. With the script above, commit 5 exited 125, Git tested through the surrounding history, and it named commit 7.
When Git reports one first bad commit, copy its SHA into culprit and run the check on that commit and its parent. Use the log to preserve the verdicts before resetting the session. The first-parent comparison below applies to linear history and to an explicit first-parent search.
culprit=PASTE_FIRST_BAD_COMMIT_SHA
git bisect log > ../checkout-bisect.log
git bisect reset
git switch --detach "$culprit^1"
node ../bisect-shipping.cjs
echo $?
git switch --detach "$culprit"
node ../bisect-shipping.cjs
echo $?
git diff "$culprit^1" "$culprit" -- shipping.cjs
You want a passing parent and a failing culprit. For an ordinary bisect that lands on a merge commit, run the test on each parent and examine the diffs against both. The bug may already be present on one branch, or it may only appear when the changes combine.
If Git returns more than one possible first bad commit
Skipping can leave a hole right at the point where the result changes. When Git lists several possible first bad commits, report that range. If a candidate stopped building because it needs a different runtime, try the version pinned at that revision and rerun the probe. If a regression was introduced, fixed, and later reintroduced, split the history at a release or commit with a confirmed result and bisect the interval you care about.
If a merge brought the bug onto main
Start a separate search with git bisect start --first-parent "$bad" "$good" when you want the merge or squash commit where the first-parent history of main became bad. Pick a good commit from that same mainline. This is useful when several agent branches were merged and each feature branch contains intermediate commits that were never deployed. For a squash merge, the individual branch commits may only be available via the original pull request or branch. Git first-parent bisect documentation
Give your coding agent the evidence
Use a prompt that names the behavior and the two checks you ran. The agent can investigate the diff, add the failing case to the project's normal tests, and propose a fix on the current branch. That keeps the historical worktree available for verification.
The $50 cart should cost $12 to ship. It costs $19 at <first-bad-sha>.
The same probe passes on <parent-sha>. A $75 cart still ships free on both.
Inspect the difference between those commits and trace the price change.
Add a test for both carts, make the smallest fix on the current branch,
and show that the $50 case fails before the fix and passes afterward.
If the changed commit is a broad refactor, inspect the behavior on today's branch before reverting it. A one-line price fix might preserve other changes that shipped with the refactor. Once you've saved the evidence and finished checking historical revisions, run git worktree remove ../checkout-bisect from your main checkout. If Git says the worktree has changes, inspect git -C ../checkout-bisect status --short and save anything you need first.
Tested on a local nine-commit example with Git for Windows and Node 22. The cart prices and commits are sample data.
For related workflows, see how to test AI-generated code when the agent writes the tests and how to run parallel coding agents with Git worktrees.