Find the commit that broke a test with git bisect
The agent changed the billing rule and two documentation patches landed around the same time. Your last release rounded up. Today, a sixteen-minute session only pays for one quarter of an hour. Give Git one repeatable question and let it find the first revision that gives the wrong answer.
- Before you start
- Git 2.28+, Node.js 22+ and a POSIX shell. Start in a new empty parent directory, then run each block in order in the same terminal.
- What to verify
- Produce the subject and diff of the first bad commit, then return to the original main checkout.
Mark a revision with known behaviour
Build a tiny meter that charges for each started block of fifteen minutes. Put a lightweight tag on this first commit. The tag gives you a readable starting point even if you make the same exercise on two machines and the commit hashes differ.
mkdir chair-lab
cd chair-lab
git init -b main
git config user.name "Lab Reviewer"
git config user.email "reviewer@example.invalid"
printf '%s\n' 'export const bill = n => Math.ceil(n / 15);' > meter.mjs
git add meter.mjs
git commit -m "Bill each started block"
git tag v1On v1 the contract is simple. Sixteen minutes must buy two blocks. Fifteen minutes would buy one block with either rounding method and would make a poor detector.
Build a short trail of suspects
Record two harmless notes and change the rounding rule between them. Separate commits matter because Git is about to classify whole snapshots of the repository. One great big commit would only tell you the entire afternoon was suspicious.
printf '%s\n' 'Meter in minutes' > notes.txt
git add notes.txt
git commit -m "Document the units"
printf '%s\n' 'export const bill = n => Math.floor(n / 15);' > meter.mjs
git commit -am "Adjust minute rounding"
printf '%s\n' 'One chair per meter' >> notes.txt
git commit -am "Clarify the seat count"
git log --oneline --reversegit log should list four subjects. In a real investigation you would know that the current branch was broken and an earlier release worked, without knowing which change sits in between.
Run the same probe at both ends
Keep the probe in the parent directory so it remains available when Git switches revisions. It imports the meter from the current checkout and exits with status 1 when the observed result is wrong. Test both endpoints before giving Git control.
cat > ../probe.mjs <<'EOF'
import { pathToFileURL } from 'node:url';
import { resolve } from 'node:path';
const { bill } = await import(pathToFileURL(resolve('meter.mjs')));
const got = bill(16);
console.log('16 minutes →', got);
process.exitCode = got === 2 ? 0 : 1;
EOF
git switch --detach v1
node ../probe.mjs
git switch main
node ../probe.mjsYou should see 2 on v1 and 1 on main. The last command in this block deliberately exits unsuccessfully. Keep the failure and move on to the next block.
Let Git choose the next revision
Pass the bad endpoint first and the good one second. Git picks intermediate commits and runs the same probe. Inspect the recorded bad revision before resetting the session. The checkout at the end of the search can be the last revision tested.
git bisect start HEAD v1
git bisect run node ../probe.mjs
git show --oneline --patch refs/bisect/bad -- meter.mjs
git bisect reset
git status --short --branchThe reported subject is Adjust minute rounding. The patch changes Math.ceil to Math.floor. After git bisect reset, git status should show main.
Try this prompt with your agent
We charge per started fifteen-minute block. Write a deterministic check for sixteen minutes and state the expected exit status on success and on failure. Once bisect identifies the revision, explain which expression changed and give two neighbouring inputs to test.A trap worth catching
Make the probe independent of the clock, network and randomness. If an older revision genuinely cannot be built or tested, exit 125 to let Git skip it. Too many skipped revisions near the boundary can leave more than one possible culprit.