Sub topic 7
Letting git bisect run do the search
This chapter assumes you can run a manual git bisect session, that you know the exit code contract (0 good, 1 to 127 except 125 bad, 125 skip, 128 and above abort), and that you have a reproducer script that you have already checked on both endpoints. Here you will hand the whole search to Git.
What git bisect run does
In a manual session you check out a commit, test it, and type git bisect good or git bisect bad. git bisect run replaces that loop. Git picks the midpoint commit, checks it out, runs your command, reads the exit status, marks the commit, and repeats until one commit is left.
The command takes the program to run and its arguments:
git bisect start HEAD v1.4.0
git bisect run ./check.sh
git bisect resetgit bisect start HEAD v1.4.0 marks HEAD as bad and the v1.4.0 tag as good. The order is bad first, then good. git bisect run ./check.sh starts the automatic search. git bisect reset ends the session and returns you to the branch you started from.
You can also give git bisect run the endpoints through a session that is already started. If you started with git bisect start and marked the endpoints by hand with git bisect bad and git bisect good v1.4.0, then git bisect run begins from that state. Git needs at least one good and one bad commit before it can run.
The loop, step by step
Each iteration follows the same steps:
- Git computes the commit that splits the remaining candidates as evenly as it can.
- Git checks out that commit, which detaches
HEAD. - Git runs your command in the root of the working tree.
- Git reads the exit status and marks the commit good, bad, or skipped.
- Git narrows the range and goes back to step 1.
When no candidates are left to test, Git prints the first bad commit and stops. The session stays open until you run git bisect reset.
Choosing what to pass as the command
The command can be a script file, but it does not have to be. Anything that Git can execute works. For short checks, call a shell inline:
git bisect run sh -c 'make || exit 125; ./run_regression_test.sh'Here sh -c runs a small program. If make fails, the shell exits with 125 and Git skips the commit. Otherwise the shell's exit status is the status of the last command, ./run_regression_test.sh. That status then decides good or bad.
Quote the whole program in single quotes. Without them, your own shell would split the command at || and ; before Git ever saw it.
You can also run a test command directly, with no wrapper at all:
git bisect run make testThis is the simplest form, but it has a trap. If make test fails because the code does not compile, the status is non-zero, and Git marks the commit bad. That blames a commit for a build failure and not for your regression. Wrap the build step and exit 125 when it fails, as shown above, whenever the history may contain broken builds.
Annotated example
Suppose a function parse_date began returning the wrong value for leap days sometime between tag v1.4.0 and main. The checker script builds the project and runs one targeted test:
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
./run_regression_test.shThe script makes two decisions. A failed build means "cannot test", so it exits 125. A passing test script exits 0 and a failing one exits non-zero, and the script passes that status straight through because it is the last command.
Before the real search, confirm the script on both endpoints:
git checkout v1.4.0 && ./check.sh; echo "good endpoint exit: $?"
git checkout main && ./check.sh; echo "bad endpoint exit: $?"You want 0 on the first line and a number from 1 to 127 (not 125) on the second. If either result is wrong, fix the script before you start. A script that fails on the good endpoint makes Git report a wrong commit with full confidence.
Then run the search from the branch you want to diagnose:
git checkout main
git bisect start HEAD v1.4.0
git bisect run ./check.shReading the output
Git prints a progress line before each step, and then the output of your script. A typical run looks like this:
running './check.sh'
Bisecting: 63 revisions left to test after this (roughly 6 steps)
[3f2a9c1...] Refactor date helpers
running './check.sh'
Bisecting: 31 revisions left to test after this (roughly 5 steps)
...
d41e7b0c5a1f2e9b8c7d6a5f4e3d2c1b0a9f8e7d is the first bad commit
commit d41e7b0c5a1f2e9b8c7d6a5f4e3d2c1b0a9f8e7d
Author: A. Developer <[email protected]>
Date: Tue Mar 3 10:12:44 2026 +0000
Use day-of-year table for February
bisect found first bad commitThe hash and message above are illustrative. The shape is what matters. The "roughly N steps" estimate is about log2 of the remaining commits. For 128 commits it takes about 7 tests, and for 1,000 commits about 10.
The line that ends in "is the first bad commit" is the answer. The next chapter covers how to inspect it.
Where the script runs and what it can touch
Git runs your command with the working tree checked out at the commit under test. That has three consequences.
- Relative paths resolve against the repository root.
./check.shworks only if the file exists at the commit being tested, or you run it from outside the tree. - Tracked files change under you. If
check.shis committed in the repository, Git replaces it with whatever version existed at each commit. Old commits may have an older version, or none at all. An absent script exits with 127 ("command not found"), which Git treats as bad. - Untracked files stay. Build output and untracked scripts are not removed by checkout, so stale objects from a previous step can leak into the next build.
The safest fix for the second problem is to keep the script outside the repository:
cp check.sh /tmp/check.sh
chmod +x /tmp/check.sh
git bisect start HEAD v1.4.0
git bisect run /tmp/check.shFor the third problem, make the script rebuild cleanly. Use a clean target, or build in a separate directory.
What stops a run
A run ends in one of four ways.
| Situation | Exit status | Result |
|---|---|---|
| Search finishes | any mix of 0, 1 to 124, 125, 126, 127 | Git prints the first bad commit |
| Script exits with 128 to 255 | 128 or above | Git stops with an error and leaves the session open |
| Only skipped commits remain | 125 on many commits | Git reports the candidates it cannot tell apart |
| Good and bad endpoints are inconsistent | any | Git refuses to start or reports an error |
When a run aborts, the session stays active at the commit that failed. You can inspect the checkout, fix the script, and run git bisect run again. Git continues from the recorded state, so earlier answers are not lost.
Signals produce statuses above 128. A script killed by a segmentation fault typically returns 139, which aborts the whole bisect. If a crash is the symptom you are hunting, catch it in the script and convert it:
#!/bin/sh
# check_crash.sh: treat a crash as a bad commit
make >/dev/null 2>&1 || exit 125
./app --self-test
status=$?
if [ "$status" -ge 128 ]; then
exit 1
fi
exit "$status"This maps any signal death to exit 1 (bad), while a normal failure code passes through unchanged. A normal failure code of 125 would still mean skip, so make sure ./app never uses 125 for its own errors.
Common mistakes
- Swapping the endpoints. If you pass the good commit as bad, Git complains about merge bases or reports nonsense. Remember the order: bad first, then good.
- Testing the wrong thing. A script that checks "the build works" finds the commit that broke the build, not the commit that broke the feature.
- Hiding failures with a pipeline. In
./test.sh | tee log.txt, the exit status is that oftee, which is almost always 0. Useset -o pipefailin a Bash script, or redirect to a file instead. - Ignoring uncommitted changes. Checkout can fail if you have local edits that conflict. Commit or stash them before you start.
- Forgetting to reset. The repository stays in a detached
HEADstate untilgit bisect resetruns.
Exercise
You run git bisect run make test and Git blames a commit whose only change is a typo in a header file, which makes the project fail to compile. The regression you were hunting is in an older commit. What went wrong, and how do you change the command so that unbuildable commits do not get blamed?
Show answer
make test returns a non-zero status when compilation fails, so Git treats the unbuildable commit as bad even though it does not show the regression. Wrap the build and test steps so that a build failure exits 125 (skip). For example: git bisect run sh -c 'make || exit 125; make test'. Git then skips the broken commit and keeps searching among the testable ones.
Exercise
A colleague's committed check.sh is in the repository. During a bisect, half the commits are marked bad immediately and the final answer looks wrong. Give the most likely cause and two ways to fix it.
Show answer
Git checks out each commit's own version of check.sh, and older commits have a different version or none at all. A missing file returns 127, which Git treats as bad. Fix it by copying the script outside the repository (for example to /tmp/check.sh) and running it by absolute path, or by pointing git bisect run at a script stored in a separate directory. Validate the script on both endpoints before starting again.