Finding the Commit That Broke Something With git bisect
Find the commit that introduced a bug with a binary search: start a bisect with good and bad commits, mark each step, automate it with git bisect run and exit codes, skip untestable commits, use custom terms, follow first parents, and save or replay a session.
kvi5hnuCode Sprout team member7 min readOn this page
Something worked in last month's release and is broken now, and there are 300 commits in between. Reading them one by one would take hours. git bisect does a binary search through history instead: you tell it one commit where things were good and one where they're bad, and it checks out the commit halfway between. You test it and say "good" or "bad", and it halves the range again. Three hundred commits take about nine steps. With a test script, Git does all the steps for you.
A bisect session
To demonstrate, here's a project with 16 commits. One of them broke calc.sh, which should print 5:
git init -q trail-guide && cd trail-guide
echo 'echo $((2 + 3))' > calc.sh; git add .; git commit -q -m "Add calculator"
git tag v1.0
for n in $(seq 2 16); do
echo "note $n" >> notes.txt
[ "$n" = 11 ] && echo 'echo $((2 + 3 + 1))' > calc.sh # the bug sneaks in here
git add .; git commit -q -m "Change $n"
done
sh calc.sh # broken now: prints 6
git bisect start
git bisect bad # the current commit is bad
git bisect good v1.0 # the release was good
echo "--- testing $(git log -1 --format=%s): $(sh calc.sh)"
git bisect good
echo "--- testing $(git log -1 --format=%s): $(sh calc.sh)"
git bisect bad
echo "--- testing $(git log -1 --format=%s): $(sh calc.sh)"
git bisect good
echo "--- testing $(git log -1 --format=%s): $(sh calc.sh)"
git bisect bad
git bisect reset > /dev/null 2>&1
6
status: waiting for both 'good' and 'bad' commits
status: waiting for 'good' commit(s), 'bad' commit known
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[61b704874ab17d0dbba832404e11e8dc9d4bd0b9] Change 8
--- testing Change 8: 5
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[b6840e279fb9dbbed953cc917bf0c35d632c4f28] Change 12
--- testing Change 12: 6
Bisecting: 1 revision left to test after this (roughly 1 step)
[2bef3514628a43c9e7b925f42e7fd8b5c01c9e9f] Change 10
--- testing Change 10: 5
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[1e54d96cb53c420104dccad62d1c4aeeab68f8b3] Change 11
--- testing Change 11: 6
1e54d96cb53c420104dccad62d1c4aeeab68f8b3 is the first 'bad' commit
commit 1e54d96cb53c420104dccad62d1c4aeeab68f8b3
Author: Sam Lee <sam@example.com>
Date: Mon Mar 2 10:00:00 2026 +0000
Change 11
calc.sh | 2 +-
notes.txt | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
git bisect startbegins a session.git bisect bad [commit]marks a commit with the bug, the current one if none is given.git bisect good <commit>marks one without it, usually a release tag or an older commit you know worked. You can give both at once:git bisect start <bad> <good>.- Git checks out a commit in the middle, in detached HEAD (article 11), and says how many revisions are left and roughly how many steps.
- Test it: build, run, click through. Then type
git bisect goodorgit bisect bad. - When one commit remains, Git prints "
<id>is the first 'bad' commit", with its message and changed files. git bisect resetends the session and returns you to the branch you started on.git bisect reset <commit>goes elsewhere instead.
The test must check the same problem every time. If a commit shows a different bug, don't let that change your answer: skip it (below).
Automating with git bisect run
If a command can tell good from bad, Git can run the whole search itself. git bisect run <command> runs the command at each step and reads its exit status:
| Exit status | Meaning |
|---|---|
0 | good |
1 to 127, except 125 | bad |
125 | can't test this commit: skip it |
128 or more, or killed by a signal | stop bisecting |
git init -q trail-guide && cd trail-guide
echo 'echo $((2 + 3))' > calc.sh; git add .; git commit -q -m "Add calculator"
git tag v1.0
for n in $(seq 2 16); do
echo "note $n" >> notes.txt
[ "$n" = 11 ] && echo 'echo $((2 + 3 + 1))' > calc.sh
git add .; git commit -q -m "Change $n"
done
cat > ../check.sh <<'EOF'
#!/bin/sh
# good (exit 0) if the calculator prints 5
[ "$(sh calc.sh)" = 5 ]
EOF
chmod +x ../check.sh
git bisect start HEAD v1.0 > /dev/null
git bisect run ../check.sh | grep -E "first 'bad' commit|^ Change"
git bisect reset > /dev/null 2>&1
1e54d96cb53c420104dccad62d1c4aeeab68f8b3 is the first 'bad' commit
Change 11
bisect found first 'bad' commit
- Keep the test script outside the repository, or untracked, so it doesn't change as Git checks out old commits. Here it's in the parent folder.
- The command can be anything:
git bisect run npm test,git bisect run make check, orgit bisect run sh -c 'make && ./run-one-test.sh'. - A test that needs a build step should exit with
125if the build fails, so broken commits are skipped instead of being counted as bad. - Often the existing test suite isn't specific enough. Write a small test for exactly this bug, and keep it outside the repository while bisecting.
Skipping commits
Some commits can't be tested: they don't build, or they fail for an unrelated reason. git bisect skip moves to a nearby commit instead. If skipped commits sit right next to the culprit, Git can only narrow it down to a list: "The first bad commit could be any of: ...".
git bisect skip # skip the current commit
git bisect skip v2.1..v2.2 # skip a whole range known to be broken
Other terms: old and new
"Good" and "bad" sound odd when you're looking for when something was fixed or changed, not broken. Use old and new, or define your own terms:
git bisect start --term-old=slow --term-new=fast
git bisect fast HEAD
git bisect slow v1.0
# ... then mark each step "slow" or "fast"
git bisect old and git bisect new work without any setup. git bisect terms shows the terms in use.
Merges and --first-parent
On a project that merges feature branches, the culprit might be a commit deep inside a branch, which may not even build on its own. git bisect start --first-parent only tests commits on the main line, the merges themselves, and tells you which merge brought the problem in. Then bisect inside that branch if you need more detail.
Saving, replaying and inspecting a session
| Command | Effect |
|---|---|
git bisect log | print the session so far, as commands |
git bisect replay <file> | repeat a saved log, such as after a wrong answer |
git bisect visualize (view) | show the remaining candidates in gitk, or with git log if no GUI is available |
git bisect reset | end the session |
Answered wrongly in step 5 of 9? Save the log, edit out the wrong line and everything after it, then reset and replay:
git bisect log > bisect.txt
# edit bisect.txt, delete the wrong step and those after it
git bisect reset
git bisect replay bisect.txt
Tips
- Find a good commit first. Check out a release tag and confirm the bug isn't there before starting. A wrong "good" sends the search in the wrong direction.
- Clean working tree. Commit or stash changes before starting (article 27), because bisect checks out other commits.
- Small commits help. Bisect points to a commit. If commits are small and focused, the culprit tells you almost exactly what broke (article 52).
- Make every commit build. Projects that squash broken intermediate commits or test every commit bisect much more easily.
- Steps. The number of steps is about log2 of the number of commits: 1,000 commits take about 10 steps.
What's current
git bisect runworks with any test command, and the exit-status rules are unchanged.--first-parent(Git 2.29) limits bisecting to the main line of merges.git bisect old/newand--term-old/--term-newmake bisect useful for any "when did this change?" question, not only bugs.
Common questions
I'm in the middle of a bisect and need to work on something else. What do I do?
git bisect log > bisect.txt to save it, git bisect reset, do your work, then git bisect replay bisect.txt to continue where you were.
Bisect said a merge commit is the first bad commit. What does that mean?
The two sides were fine on their own, but combining them broke something, typically through a conflict resolution or an interaction between the changes. Look at the merge with git show --remerge-diff <merge>, or at what each side changed.
Can bisect find when a bug was fixed?
Yes. Use git bisect start --term-old=broken --term-new=fixed, then mark commits broken or fixed. Or use old and new.
Why did my git bisect run stop with "bisect run failed"?
The command exited with status 128 or above, or was killed, which tells Git to stop. Check that the script runs on its own, and that it returns 1 for bad and 125 for untestable commits, not other high values.
Summary
git bisect start,git bisect bad,git bisect good <commit>begin a binary search. Mark each checked-out commit good or bad until Git names the first bad commit.git bisect run <command>automates it: exit0means good,1-127bad (except125, skip), and128+ aborts.git bisect skiphandles untestable commits.old/newor custom terms suit non-bug questions.--first-parentbisects only the main line of merges.git bisect logandreplaysave and repeat sessions, andgit bisect resetends one.
Practice questions
There are about 500 commits between the last good release and today. Roughly how many test runs does a bisect need?
Answer
About 9: each step halves the range, and 2^9 = 512. Skipped commits add a few more.
Write a git bisect run script for a bug where make test-login fails, but where some old commits don't compile at all.
Answer
#!/bin/sh
make > /dev/null 2>&1 || exit 125 # can't build: skip this commit
make test-login # exit 0 = good, non-zero = bad
Save it outside the repository, make it executable, then run git bisect start <bad> <good> and git bisect run /path/to/script.sh. The build failure exits with 125, so those commits are skipped instead of being marked bad. Note that if make test-login itself could exit with 125 or 128+, wrap it to return 1 on failure.
Halfway through a bisect, you realize you marked a commit "good" by mistake. How do you recover without starting over?
Answer
git bisect log > bisect.txt, edit the file to delete the wrong git bisect good line and every line after it, then git bisect reset and git bisect replay bisect.txt. Git rebuilds the session up to just before the mistake, and you continue from there.
Previous: Who Changed This Line? git blame and Ignoring Noise Commits
Next: Searching and Statistics: git grep, shortlog, .mailmap and Repository Numbers

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.