Git Bisect Tutorial - Find the Commit That Broke Your Code with git bisect run, Exit Code 125 to Skip, and the Wrong-Mark Mistake (Real Output from git 2.43.0)

TL;DR

  • git bisect is a binary search over your history. You give it one bad commit and one good commit; it checks out the commit halfway between and asks whether that one is good or bad, then halves the range again. The git bisect reference ends the process with "a description of the first bad commit" and leaves refs/bisect/bad pointing at it.
  • The cost is roughly log2(N) tests. In my runs git 2.43.0 needed 5 tests for 20 commits and 10 tests for 1,000 commits (section 6).
  • The fast path is git bisect start <bad> <good> then git bisect run ./test.sh. The script must exit 0 for good, 1 to 127 (except 125) for bad, 125 to skip an untestable commit; any other code aborts the run.
  • Always finish with git bisect reset. It returns you to the branch you started on (section 3).
  • One wrong good or bad gives a confident wrong answer. I marked a bad commit as good on purpose and git printed commit 16 instead of commit 13 (section 7). git bisect log plus git bisect replay is the fix.
  • Not your case? If you already know the bad commit and want to rewrite or remove it, read git reset soft, mixed and hard; to run your tests on every commit before you push instead of after, read git rebase --exec.

Table of Content

  1. What git bisect does
  2. Set up a repository with a known bug
  3. Bisect by hand
  4. Automate it with git bisect run
  5. Exit codes: 125 skips, above 127 aborts
  6. How many steps does it take
  7. What broke when I ran this
  8. Log, replay, terms and the other options
  9. Conclusion

1. What git bisect does

Something worked last week and does not work today, and 20, 200 or 2,000 commits landed in between. Reading every diff is slow. git bisect uses the fact that history is a straight line you can search: if commit 1 is good and commit 20 is bad, test commit 10. If 10 is good, the bug arrived somewhere in 11 to 20, so test 15. Each answer throws away half of the remaining candidates. The reference page puts it this way: git "picks a commit between those two endpoints and asks you whether the selected commit is 'good' or 'bad'", and keeps narrowing until there are "no more revisions left to inspect".

git bisect testing commits 10, 15, 12, 14 and 13 in a 20 commit history

Here are the subcommands I use, checked against the reference page:

CommandWhat it does
git bisect start <bad> <good>Starts a session with both endpoints and checks out the midpoint
git bisect good / git bisect badMarks the commit that is checked out now (or a named revision)
git bisect run <cmd>Runs the command at every step and marks the commit from its exit code
git bisect skipMarks the current commit as untestable
git bisect logPrints every mark you made so far
git bisect replay <file>Re-applies a saved log, for example after fixing a wrong mark
git bisect visualizeShows the remaining suspects in gitk, or in git log when there is no display
git bisect resetEnds the session and returns to the commit you started from

Bisect is read-only with respect to your history: it only moves HEAD around and writes refs under refs/bisect/. Your commits are not changed. What it does do is overwrite your working tree at each step, so commit or stash uncommitted work before you start.

2. Set up a repository with a known bug

I want the failing commit to be known in advance so you can check the answer. I built a repository with 20 commits. Every commit writes a line to notes.txt, and total.txt holds the number 100 up to commit 12 and the number 99 from commit 13 on. Commit 1 carries the tag v1.0. The check script is two real lines:

1#!/bin/sh
2# exit 0 when the report total is 100, exit 1 otherwise
3total=$(cat total.txt)
4[ "$total" = "100" ]

I ran everything below with git 2.43.0 on 2026-12-04 (UTC). First, the check on the newest commit:

1$ git --version
2git version 2.43.0
3$ ./test.sh; echo "exit=$?"
4exit=1

exit=1 means HEAD is bad. v1.0 is good because it still has the 100. Those are my two endpoints.

3. Bisect by hand

Start the session with the bad revision first and the good one second. After each checkout I ran ./test.sh myself and typed git bisect good or git bisect bad accordingly. This is the output of the five steps on 2026-12-04 with git 2.43.0:

 1$ git bisect start HEAD v1.0
 2Bisecting: 9 revisions left to test after this (roughly 3 steps)
 3[37cf159601c3a7d4c05ae2c944af14fd17fe8119] commit 10: update notes
 4$ ./test.sh && git bisect good || git bisect bad
 5Bisecting: 4 revisions left to test after this (roughly 2 steps)
 6[28ff1cac388ad0b4567ee551d2a1254867a70fe4] commit 15: update notes
 7$ ./test.sh && git bisect good || git bisect bad
 8Bisecting: 2 revisions left to test after this (roughly 1 step)
 9[5881def57052d8583e79fbda9004f85d7ce76cb3] commit 12: update notes
10$ ./test.sh && git bisect good || git bisect bad
11Bisecting: 0 revisions left to test after this (roughly 1 step)
12[dba7b99e9b7b82d048e401097ca48dc6ba65b3b1] commit 14: update notes
13$ ./test.sh && git bisect good || git bisect bad
14Bisecting: 0 revisions left to test after this (roughly 0 steps)
15[bca0147fe631053c6fbbe929c17ba0bf1145d9bd] commit 13: update notes
16$ ./test.sh && git bisect good || git bisect bad
17bca0147fe631053c6fbbe929c17ba0bf1145d9bd is the first bad commit
18commit bca0147fe631053c6fbbe929c17ba0bf1145d9bd
19Author: Rahul <r@example.com>
20Date:   Fri Dec 4 05:06:43 2026 +0000
21
22    commit 13: update notes
23
24 notes.txt | 2 +-
25 total.txt | 2 +-
26 2 files changed, 2 insertions(+), 2 deletions(-)

Two things in that output are worth reading carefully. First, the "roughly N steps" estimate is exactly that, an estimate: the first line said 3 steps and I needed 5 tests in total, because the estimate is about the steps still to come after the current checkout. Second, git does not stop when the midpoint is bad; it keeps going until the range is one commit wide. Commit 13 changed total.txt from 100 to 99, which is the bug I planted.

The session is still open at this point. git status would say you are on a detached HEAD at the first bad commit. To get back:

1$ git bisect reset
2Previous HEAD position was bca0147 commit 13: update notes
3Switched to branch 'main'

By default, as the reference says, the reset "will return your tree to the commit that was checked out before git bisect start". If you want a different landing place, git bisect reset <commit> takes one. If you forget the reset, your next commit lands on a detached HEAD, which is how people lose work, so I treat the reset as part of the command, like closing a file.

4. Automate it with git bisect run

Typing good and bad five times is fine for 20 commits and miserable for 200. git bisect run <cmd> does the loop for you: it runs the command at every checkout and marks the commit from the exit code. I used the same test.sh (output on 2026-12-04 with git 2.43.0, lines of the changed-file summary trimmed):

 1$ git bisect start HEAD v1.0
 2$ git bisect run ./test.sh
 3running './test.sh'
 4Bisecting: 4 revisions left to test after this (roughly 2 steps)
 5[28ff1cac388ad0b4567ee551d2a1254867a70fe4] commit 15: update notes
 6running './test.sh'
 7Bisecting: 2 revisions left to test after this (roughly 1 step)
 8[5881def57052d8583e79fbda9004f85d7ce76cb3] commit 12: update notes
 9running './test.sh'
10Bisecting: 0 revisions left to test after this (roughly 1 step)
11[dba7b99e9b7b82d048e401097ca48dc6ba65b3b1] commit 14: update notes
12running './test.sh'
13Bisecting: 0 revisions left to test after this (roughly 0 steps)
14[bca0147fe631053c6fbbe929c17ba0bf1145d9bd] commit 13: update notes
15running './test.sh'
16bca0147fe631053c6fbbe929c17ba0bf1145d9bd is the first bad commit
17bisect found first bad commit
18$ git rev-parse refs/bisect/bad | cut -c1-7
19bca0147
20$ git show -s --format=%s refs/bisect/bad
21commit 13: update notes

It found the same commit as the manual run with no typing. The reference page shows the same pattern with a build: git bisect start HEAD v1.2 --, then git bisect run make, then git bisect reset. The trailing -- separates revisions from paths, so a file that happens to share a name with a tag cannot confuse the parser.

The test command does not have to be a shell script. Anything that returns an exit code works: git bisect run make, git bisect run npm test, git bisect run python -m pytest tests/test_report.py -x. Two habits make it reliable:

  • Keep the script outside the tracked files or commit it before the range starts. My test.sh is in commit 1 so it exists at every checkout. If the script lives in the commit that is being tested and it did not exist yet, you get a "No such file" error at the older commits, which exits with 127 and aborts the run (see the next section).
  • Test one behaviour. A script that checks five things tells you the first commit where any of them failed, which may not be the bug you were chasing.

5. Exit codes: 125 skips, above 127 aborts

The reference defines the contract for the command you pass to run:

Exit codeMeaning to git bisect
0The commit is good (old)
1 to 127, except 125The commit is bad (new)
125The commit cannot be tested: skip it
128 and aboveAbort the whole bisect

The page explains why 125 was chosen: "126 and 127 are used by POSIX shells to signal specific error status." This matters in practice. A commit in the middle of the range may not compile, and if your script just exits 1 on a build failure, git marks the commit bad and points at the wrong place. The documented fix is git bisect run sh -c "make || exit 125; ~/check_test_case.sh", so a build failure becomes a skip and only a real test failure becomes bad.

I tested both edges. To simulate an unbuildable commit 13 I wrote a script that exits 125 when notes.txt says change 13 and otherwise runs the same check. Output on 2026-12-04 with git 2.43.0 (tail of the run):

 1$ git bisect run ./test125.sh
 2...
 3running './test125.sh'
 4Bisecting: 0 revisions left to test after this (roughly 0 steps)
 5[bca0147fe631053c6fbbe929c17ba0bf1145d9bd] commit 13: update notes
 6running './test125.sh'
 7There are only 'skip'ped commits left to test.
 8The first bad commit could be any of:
 9bca0147fe631053c6fbbe929c17ba0bf1145d9bd
10dba7b99e9b7b82d048e401097ca48dc6ba65b3b1
11We cannot bisect more!
12error: bisect run cannot continue any more

That is the honest behaviour, and it matches the caveat in the reference: "if you skip a commit adjacent to the one you are looking for, Git will be unable to tell exactly which of those commits was the first bad one." Git gave me two candidates, 13 and 14, instead of a wrong single answer. The bug is in one of them, and I would read both diffs.

The abort edge, with a script that always exits 200:

1$ git bisect run ./t200.sh
2running './t200.sh'
3error: bisect run failed: exit code 200 from './t200.sh' is < 0 or >= 128

No mark was written; git bisect log afterwards showed only my start line. The reference also warns that exit(-1) becomes status 255 because the value is "chopped with & 0377", so a C program that returns -1 aborts the run.

6. How many steps does it take

The reference gives only examples ("675 revisions left to test after this (roughly 10 steps)") and no formula. The count is a binary search, so roughly log2(N). I measured it on a second repository with 1,000 commits where v holds the commit number and the test fails once it is 700 or higher:

1$ git bisect start HEAD $(git rev-list --max-parents=0 HEAD)
2Bisecting: 499 revisions left to test after this (roughly 9 steps)
3$ git bisect run ./t.sh | grep -c '^running'
410
5$ git show -s --format=%s refs/bisect/bad
6c700

That was git 2.43.0 on 2026-12-04: the whole git bisect run over 1,000 commits took 75 ms wall time (38 ms for the 20-commit repository), because the test is a one-line shell check and each checkout rewrites only one tiny file; the 1,000-commit repository was 14 MB on disk according to du -sh .git. In a real project the test dominates: with a 5-minute build, 10 tests cost 50 minutes and 300 tests would cost 25 hours. 10 tests found commit 700 out of 1,000, and 5 tests found commit 13 out of 20. A linear walk from the top would have needed about 300 tests for the 1,000-commit case. This is the whole reason to learn bisect: the cost grows with the logarithm of the history, so a range 100 times longer costs about 7 more tests.

Commits in rangeTests I countedlog2(N), rounded up
2055
1,0001010

7. What broke when I ran this

  • One wrong mark gave a confident wrong answer. I wanted to see what a typo does. After git bisect start HEAD v1.0 and a correct good at commit 10, git checked out commit 15, whose test fails. I typed good anyway. The remaining steps were consistent with that lie and git finished with this (2026-12-04, git 2.43.0):

    1$ git bisect good
    2Bisecting: 2 revisions left to test after this (roughly 1 step)
    3[9cbc5fc754eeee7f80575018f968745e0da6edc6] commit 17: update notes
    4...
    5a386500b9954ea108341fdd27e7a34ccd301d46c is the first bad commit
    6commit a386500b9954ea108341fdd27e7a34ccd301d46c
    

    That is commit 16, three commits after the real culprit, and git printed it with the same certainty as the correct answer. Nothing in the output warns you. The recovery is section 8: git bisect log > fixed.log, delete the wrong line, git bisect reset, git bisect replay fixed.log.

  • Marking the starting commit is easy to get backwards. After git bisect start HEAD with no good commit, git only says status: waiting for good commit(s), bad commit known and does not check anything out until you give it a good revision. I read that line twice before I understood it was not an error.

  • A flaky test is the worst input. If the check fails one time in ten, a single false bad mark early on throws away the half of history that contains the answer. For anything flaky I repeat the check inside the script (three passes in a row for good) rather than trust one run.

  • Untracked helper scripts survive checkouts, tracked ones do not. My test125.sh and t200.sh were untracked, so they were present at every commit. After git bisect reset, git status --short still listed them as ??, which is what I wanted, but it means a script that is tracked in only some commits can disappear mid-run.

8. Log, replay, terms and the other options

Log and replay. git bisect log prints every mark so far. This is the log of my manual run, which is also the thing to paste into a bug report:

 1$ git bisect log
 2# bad: [e0eaaeaebacd3e3cfb0421f1bd43772c02960484] commit 20: update notes
 3# good: [6e587a417edd89b97558a55e54ffd63e07bbcd23] commit 1: update notes
 4git bisect start 'HEAD' 'v1.0'
 5# good: [37cf159601c3a7d4c05ae2c944af14fd17fe8119] commit 10: update notes
 6git bisect good 37cf159601c3a7d4c05ae2c944af14fd17fe8119
 7# bad: [28ff1cac388ad0b4567ee551d2a1254867a70fe4] commit 15: update notes
 8git bisect bad 28ff1cac388ad0b4567ee551d2a1254867a70fe4
 9# good: [5881def57052d8583e79fbda9004f85d7ce76cb3] commit 12: update notes
10git bisect good 5881def57052d8583e79fbda9004f85d7ce76cb3
11# bad: [dba7b99e9b7b82d048e401097ca48dc6ba65b3b1] commit 14: update notes
12git bisect bad dba7b99e9b7b82d048e401097ca48dc6ba65b3b1
13# bad: [bca0147fe631053c6fbbe929c17ba0bf1145d9bd] commit 13: update notes
14git bisect bad bca0147fe631053c6fbbe929c17ba0bf1145d9bd
15# first bad commit: [bca0147fe631053c6fbbe929c17ba0bf1145d9bd] commit 13: update notes

To fix a wrong mark, save that output to a file, edit out the wrong entry, then run git bisect reset and git bisect replay that-file, exactly as the reference describes.

Old and new instead of good and bad. When you are hunting the commit that fixed something, "bad" reads backwards. Use custom terms. I ran the session start on 2026-12-04 with git 2.43.0:

1$ git bisect start --term-old broken --term-new fixed
2$ git bisect terms
3Your current terms are broken for the old state
4and fixed for the new state.

You cannot mix good and bad with old and new in one session, according to the reference.

Merge commits: --first-parent. On a repository that merges feature branches, a broken intermediate commit on a branch can produce a false positive. git bisect start --first-parent follows only the first parent at every merge, so the merge commit itself is reported as the point where the bug came in and the commits inside the merged branch are ignored. Use it when your history is a series of merged pull requests and you only need to know which pull request did it; then bisect inside that branch as a second pass. I did not build a merge history for this post, so I am quoting the documented behaviour here, not showing output.

No checkout: --no-checkout. With git bisect start --no-checkout, git only updates the reference BISECT_HEAD and leaves your working tree alone. It suits a test that reads the commit through git itself (for example git grep or git show BISECT_HEAD:file). It is assumed in a bare repository and cannot be combined with --reset-when-found.

Visualize. git bisect visualize opens gitk when DISPLAY is set (or the Windows and macOS equivalents the reference lists) and otherwise falls back to git log, where you can pass -p or --stat. It is a quick way to look at the remaining suspects when you are bisecting by hand over SSH.

The command git rev-list --max-parents=0 HEAD in my 1,000-commit run prints the root commit, as the git rev-list reference describes for --max-parents. If you want to see which commits are still in the suspect range during a session, the git log reference covers the options visualize falls back to, and the original design write-up is the git bisect lk2009 page in the official docs. For a longer worked example, the Pro Git chapter on debugging with Git is the official walkthrough.

9. Conclusion

My rule: if I know which release worked and which one does not, I bisect before I read a single diff. The recipe I keep in my notes is five lines:

1git stash push -u -m "before bisect"     # clean tree first
2git bisect start <bad> <good> --
3git bisect run ./check.sh                # exit 0 good, 1 bad, 125 skip
4git bisect log > bisect.log              # keep the evidence
5git bisect reset                         # always

Small commits make the result useful: the answer is one commit, so a commit that mixes a refactor with a behaviour change gives you a first bad commit that is still hard to read. That is another reason to keep history tidy with commit --amend, squashing noisy fixups before you merge, and branches that stay focused. And if you want the failure caught before it is pushed, run the test on every commit with rebase --exec so you never need to bisect that range.

This is lesson Day 13 of the DevOps Master Course on YouTube; the written companion for the whole course is the free Git and GitHub blueprint course. The complete option list is on the official git bisect page.

Did this save you a debugging session? Stay in the loop -