DEV Community

Amaresh Pelleti
Amaresh Pelleti

Posted on Originally published at devtoolhub.com

Git Bisect Run: Find the Bad Commit Automatically

Originally published on DevToolHub.

Git bisect finds the commit that broke something by binary search. You mark one commit good and one bad, and Git checks out the middle one for you to test. Add git bisect run and a test script, and it does the whole search by itself. In my test, a bug planted in a 1,001-commit repository took 10 script runs to find.

I ran everything below on Git 2.56.1 in throwaway repositories. The outputs are real. The last section lists what I did not test.

In this guide:

  • Run a full automatic search with git bisect run
  • Know what each exit code from your script means
  • Handle skipped commits, fixes, and the errors Git throws
  • Undo a wrong answer with log and replay

How does git bisect find a bad commit?

Git bisect halves the range of suspect commits at every step. With a known good commit and a known bad one, Git checks out the midpoint. You test it and answer good or bad. Each answer discards half of the remaining commits, so 1,001 commits need about 10 tests.

I built a repository with 1,001 commits. In commit 613, config.txt changed from rate=100 to rate=0. Then I started the search:

git bisect start HEAD $(git rev-list --max-parents=0 HEAD)
Enter fullscreen mode Exit fullscreen mode

Git answered with the size of the job: Bisecting: 499 revisions left to test after this (roughly 9 steps). It then checked out c500, the middle of the range. The $(...) part uses git rev-list --max-parents=0 to find the first commit in the repository, which I knew was good. If you know a better good commit, such as the last release tag, use that. A smaller range means fewer steps.

How do you automate it with git bisect run?

Write a script that exits 0 when the code is good and non-zero when it is bad. Then run git bisect run ./your-script. Git checks out each midpoint, runs the script and records the answer. When the range is down to one commit, it prints that commit.

My script checked the config value:

#!/bin/sh
grep -q "rate=100" config.txt
Enter fullscreen mode Exit fullscreen mode

I saved it outside the repository, so no checkout could change it. Then I ran it:

git bisect run ../test_outside.sh
Enter fullscreen mode Exit fullscreen mode

The result, after 10 runs of the script (I counted them with a log file):

5d15229123825090b603a3db25fe8c67af8d342f is the first 'bad' commit
Author: tester <t@example.com>

    c613

 config.txt | 2 +-
 log.txt    | 1 +
Enter fullscreen mode Exit fullscreen mode

So that is exactly the commit I planted. Run git bisect reset afterward. It returns you to the branch you started on.

Tip: Keep the test script outside the repository, or untracked. Every step checks out a different commit, and a script that lives in history can change or vanish at some of them.

What do the exit codes mean in git bisect run?

Exit 0 means good. Exit 1 to 127 means bad, except 125, which means "cannot test this commit, skip it". Any code above 127 aborts the search. These rules come from the official git bisect docs, and I tested the edges.

Script exits with Git bisect treats it as
0 good (old)
1 to 127, except 125 bad (new)
125 skip this commit
128 and above abort the search

My script that exited with 130 stopped the run at once:

error: bisect run failed: exit code 130 from '.../abort.sh' is < 0 or >= 128
Enter fullscreen mode Exit fullscreen mode

The most common mistake sits just below that range. A script without execute permission exits with 126, and Git rejects it:

.../noexec.sh: Permission denied
error: bogus exit code 126 for 'good' revision
Enter fullscreen mode Exit fullscreen mode

The docs do not mention this case, so run chmod +x on the script first.

Warning: A crash in your test can look like a bad commit. If the script fails for a reason unrelated to the bug, such as a missing tool or a build error, Git records "bad". Make the script return 125 for anything it cannot test.

What happens when commits are skipped?

Skipping a commit tells Git to test a neighbor instead. But it fails once the skipped commits sit next to the culprit. Then Git cannot say which one is first. In my test, the script returned 125 for commits 610 through 615, and the bug was in 613.

Git gave up and listed suspects:

There are only 'skip'ped commits left to test.
The first 'bad' commit could be any of:
...
We cannot bisect more!
error: bisect run cannot continue any more
Enter fullscreen mode Exit fullscreen mode

It listed seven commits, c610 through c616, not one. The docs warn about this: skipping a commit adjacent to the one you want means Git cannot tell exactly which was first bad. The fix is to make the untestable commits testable, for example by applying a small patch inside the script, then returning 125 only when that fails.

How do you find a fix with git bisect?

Use custom terms. git bisect start --term-old=broken --term-new=fixed flips the question from "which commit broke it" to "which commit fixed it". The script still exits 0 for the old state, here "broken".

I tested it on a repository where commit f9 fixed a bug:

git bisect start --term-old=broken --term-new=fixed
git bisect fixed HEAD
git bisect broken $(git rev-list --max-parents=0 HEAD)
git bisect run ../isbroken.sh
Enter fullscreen mode Exit fullscreen mode

The script was grep -q broken state.txt && exit 0 || exit 1. Git printed 43c3317... is the first 'fixed' commit with the message f9 (fix lands here). The command git bisect terms shows the names in use. Custom terms are also handy for performance questions, such as "which commit made it faster".

Which errors will you hit?

Most errors come from the working tree or the script. For example, the first one stops you before the search begins.

A dirty working tree. I edited a tracked file and started a bisect. Git refused at the first checkout:

error: Your local changes to the following files would be overwritten by checkout:
    config.txt
Please commit your changes or stash them before you switch branches.
Aborting
Enter fullscreen mode Exit fullscreen mode

So commit or stash first. If you are unsure where your work went, see how to recover lost commits.

A wrong answer halfway. If you marked a commit good by mistake, you do not need to start over. Save the log, fix it and replay it.

git bisect log > bisect.log
# edit bisect.log, delete the wrong line
git bisect reset
git bisect replay bisect.log
Enter fullscreen mode Exit fullscreen mode

git bisect log printed five lines for my short session. After a reset and a replay, Git came back to the same state: 499 revisions left to test after this (roughly 9 steps).

What about merge commits and git bisect --first-parent?

The docs describe --first-parent for merged branches. It follows only the first parent at each merge. If a merged branch contained broken or non-buildable commits but the merge itself was fine, the merge commit is the one reported, and its ancestors are ignored. That avoids false positives.

I tried to reproduce the false positive. I built two histories with a merged branch that was broken in the middle and fixed at the end. In both, plain git bisect and --first-parent landed on the same real regression. So I cannot show you the difference, only tell you what the docs promise. The search depends on which commit Git picks as each midpoint, and my layouts did not trigger it. Try --first-parent yourself if your history has many merged feature branches.

Bisect also pairs well with small commits that each build. For tips on writing them, see the commit message conventions guide, and for the related undo commands, reset vs revert vs restore.

What I did not test

I used local repositories with synthetic commits and one planted bug. I did not test --no-checkout, --reset-when-found, bisecting with a pathspec, git bisect visualize, bisecting a real project with a slow build, or flaky tests. I also could not reproduce the --first-parent false positive. Commit SHAs in your repository will differ from mine, and so will the exact step counts.

Frequently Asked Questions

Q: What does git bisect do?
A: It finds the commit that introduced a change by binary search. You give it a good commit and a bad commit, and it checks out the middle for you to test. In my test it needed 10 script runs to search 1,001 commits.

Q: How do I run git bisect automatically?
A: Use git bisect run ./script. The script must exit 0 for good, 1 to 127 (except 125) for bad, and 125 to skip. Keep the script outside the repository and make it executable.

Q: What does exit code 125 mean in git bisect run?
A: It means the commit cannot be tested, so Git skips it. If you skip commits next to the culprit, Git lists several candidates instead of one, as it did with seven commits in my test.

Q: How do I stop git bisect and go back?
A: Run git bisect reset. It returns you to the commit you had checked out before the search started.

Quick Summary:

  • Git bisect halves the commit range at each test, so 1,001 commits took 10 script runs in my test
  • git bisect run ./script automates it: exit 0 is good, 1 to 127 except 125 is bad, 125 skips, 128 and above aborts
  • A script without execute permission fails with bogus exit code 126
  • Skipping commits next to the culprit leaves Git listing several suspects, seven in my test
  • --term-old and --term-new let you search for the commit that fixed something
  • Commit or stash local changes first, and keep the test script outside the repository

For the git bisect workflow, write a script that exits 0 or 1, make it executable, keep it outside the repo, and let git bisect run find the commit.

Top comments (0)