Blog / git
Coding agent broke the tests? Find the commit with git bisect
git bisect run tests commits for you until it names the first bad one. With a check old commits lack, with local edits, and how to undo it.
On this page
- Let git run the test
- The check isn't in the old commits
- When a person has to judge each commit
- You know which line changed
- You have uncommitted changes
- Undo just that commit
- Make the next one easier to find
- Several agents, one branch
- Frequently asked questions
- How do I find which commit broke my tests?
- What exit code skips a commit in git bisect run?
- Can I bisect with a test that only exists in my latest commit?
- Can I bisect with uncommitted changes?
- Try it with your team
You left an agent working, came back to a branch with thirty small commits, and a test that passed this morning now fails. Reading every diff takes an hour. Git can find the commit for you in a few minutes, by running the test itself.
We tested everything below with git 2.43 on a scratch repo: one good commit, then thirteen agent commits, one of which quietly broke add(2, 3).
Let git run the test#
You need one commit where the test passed (the start of the agent's work is usually fine) and one where it fails (usually HEAD):
git bisect start HEAD <good-commit>
git bisect run node test.jsGit checks out the commit halfway between, runs node test.js, and reads the exit code: 0 means good, 1 to 127 (except 125) means bad. It halves the range again until one commit is left. Fourteen commits took four runs:
c737fd605d738c44ae0cec323c7eefd8aabaa9e2 is the first bad commit
commit c737fd605d738c44ae0cec323c7eefd8aabaa9e2
agent: tidy add()
add.js | 2 +-
bisect found first bad commitThen go back to where you were:
git bisect resetAny command works in place of node test.js: npm test, pytest tests/, cargo test add. Run the narrowest test that shows the bug: it runs once per step.
The check isn't in the old commits#
Often the test that catches the bug is one you just wrote, so the older commits don't have it. Put it in a script outside the repo, so checking out an old commit doesn't touch it:
cat > ../check.sh <<'EOF'
#!/bin/sh
[ -f add.js ] || exit 125
node -e "process.exit(require('./add')(2,3) === 5 ? 0 : 1)"
EOF
chmod +x ../check.sh
git bisect start HEAD <good-commit>
git bisect run ../check.shThat found the same commit. Exit code 125 tells git "can't test this one, skip it", for commits where the project doesn't build or the file is missing. If the skipped commits sit right next to the bad one, git can't decide and says so:
There are only 'skip'ped commits left to test.
The first bad commit could be any of:
281c1fb929df74c1ab4bb0accd2292103b0c39e8
c737fd605d738c44ae0cec323c7eefd8aabaa9e2
We cannot bisect more!Then look at those two by hand.
When a person has to judge each commit#
Some bugs have no test yet: a page that looks wrong, a command that feels slow. Bisect by hand instead of with run. Git checks out a commit, you look, and you tell it what you saw:
git bisect start HEAD <good-commit>
git bisect bad # this one shows the bug
git bisect good # this one doesn'tAfter each answer git checks out the next commit to try:
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[849a2aedf821beff9059a19638954f0e7a3bd5f9] agent: step 3Keep answering until it prints "is the first bad commit". If you can't tell for one commit, git bisect skip moves on, the same as exit code 125. Once you have the bad commit, write the test that would have caught it.
You know which line changed#
If you already know the line that's wrong, you don't need bisect at all. git log -S lists the commits that added or removed a piece of text:
git log --oneline -S "b > 2" -- add.jsc737fd6 agent: tidy add()It is the fastest search when the bug is one odd line. Bisect is for when you only know the result is wrong.
You have uncommitted changes#
Bisect checks out other commits, so it stops when your own edits would be overwritten:
error: Your local changes to the following files would be overwritten by checkout:
notes.js
Please commit your changes or stash them before you switch branches.
AbortingRun it in a separate worktree instead. Your folder, and any agent still working in it, stays as it is:
git worktree add --detach ../bisect HEAD
cd ../bisect
git bisect start HEAD <good-commit>
git bisect run ../check.sh
git bisect reset
cd - && git worktree remove ../bisectThat found the same commit while the main folder kept its edit. Don't bisect in a folder an agent is working in: every step swaps the files under it.
Undo just that commit#
Once you know the commit, undo it without touching the rest of the agent's work:
git revert --no-edit c737fd6
node test.jsokgit revert adds a new commit that reverses the bad one, so it is safe on a branch others have pulled. If later commits built on the bad change, the revert stops with a conflict and you fix it by hand.
Make the next one easier to find#
Bisect works best when each commit does one thing and the tests pass at every commit. Ask the agent for that in your project instructions: "commit after each step, run the tests before each commit". A thirty-commit branch where every commit passes takes five runs to bisect. One giant commit takes zero, and tells you nothing.
If an agent's history went missing altogether rather than going wrong, see recover work after an agent ran git reset. To stop an agent finishing with red tests at all, see make Claude Code run the tests before it says it's done.
Several agents, one branch#
When two people's agents both push to one branch, "which commit broke it" is also "whose agent was it". Joxo shows each session's work and who started it on your phone and desktop, so you can ask the right agent to fix it. See reviewing PRs from several agents.
Frequently asked questions#
How do I find which commit broke my tests?#
Run git bisect start HEAD followed by a commit where the tests passed, then git bisect run with your test command. Git tests the middle commit each time and prints "is the first bad commit". Finish with git bisect reset.
What exit code skips a commit in git bisect run?#
- Exit 0 marks the commit good, 1 to 127 except 125 marks it bad.
Can I bisect with a test that only exists in my latest commit?#
Yes. Put the test in a script outside the repository, or in a file git doesn't track, and pass that script to git bisect run.
Can I bisect with uncommitted changes?#
Not in the same folder: the checkout stops. Create a separate worktree with git worktree add --detach and bisect there.