Git Bisect Explained: Find the Bad Commit in About 10 Checks
Git bisect explained: mark one bad commit and an older good one, and Git's binary search finds which of the last thousand commits broke your code in about ten checks, not one by one. Each answer throws out about half the suspects. In the manual's own example, 675 revisions left becomes 337, and that example goes back to Linus Torvalds's original git bisect commit from July 2005.
Then the surprise: you don't have to answer yourself. git bisect run takes your test script and reads its exit code: 0 means good, 1 to 127 means bad, 125 means skip this commit, and anything above 127 stops the search. We also cover custom terms like fast and slow for finding the commit that made things slow, and the catches: one wrong answer sends the search off course, your test has to be reliable, skipping a commit next to the culprit leaves you a list of suspects, and bisect needs one commit that still worked. It ends with the five commands to run and why small commits make bisect more effective.
Cheat sheet
What git bisect does
git bisect is a binary search over your history. You give it one bad commit and one older good commit, it checks out a commit between them and asks good or bad, and each answer throws out about half the suspects. Source: the git bisect manual.
The manual loop
The manual's own example:
git bisect start
git bisect bad # Current version is bad
git bisect good v2.6.13-rc2 # v2.6.13-rc2 is known to be good
Git checks out a commit in the middle of the range and prints something like Bisecting: 675 revisions left to test after this (roughly 10 steps). Test that commit, answer git bisect good or git bisect bad, and repeat until Git names the commit.
git bisect reset
git bisect reset returns you to the commit that was checked out before git bisect start.
Let a script answer
git bisect start
git bisect bad
git bisect good <commit that worked>
git bisect run <your test>
git bisect reset
git bisect run runs your command at every step and reads its exit code:
| Exit code | What Git does |
|---|---|
| 0 | Marks the commit good |
| 1 to 127, except 125 | Marks the commit bad |
| 125 | Skips the commit: it cannot be tested |
| 128 to 255 | Stops the bisect |
The manual's example script, which skips commits that do not build:
#!/bin/sh
make || exit 125 # this skips broken builds
~/check_test_case.sh # does the test case pass?
git bisect start HEAD HEAD~10 -- # culprit is among the last 10
git bisect run ~/test.sh
git bisect reset # quit the bisect session
The same thing on one line, with no script file:
git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
The manual says it is safer to keep the test scripts outside the repository.
Not only for bugs
Pick your own terms to find the commit that changed any property:
git bisect start --term-old fast --term-new slow
git bisect start --term-new fixed --term-old broken
You cannot mix good and bad with old and new in one session.
Fix a wrong answer
Save the output of git bisect log to a file, remove the wrong entries, then:
git bisect reset
git bisect replay that-file
Fewer checks
- Limit the search to the paths you suspect:
git bisect start -- arch/i386 include/asm-i386 - Give several known-good commits at the start.
- Skip a whole range:
git bisect skip v2.5..v2.6
How fast it narrows
| Git prints | Estimate | Where |
|---|---|---|
| 675 revisions left | roughly 10 steps | The manual's example |
| 337 revisions left | roughly 9 steps | The manual's example, one answer later |
| 10928 revisions left | roughly 14 steps | Linux kernel v2.6.25 to v2.6.27, in Git's bisect paper |
The count is what is left after the commit just checked out.
Catches
- One wrong answer. The Linux kernel's guide warns that getting it wrong just once sends the rest of the bisection totally off course.
- An unreliable test. The result is only as accurate as the test. Chasing a kernel that booted only sometimes, Nathan Chancellor passed a commit only when it booted ten times out of ten.
- A crashing test. An exit code from 128 to 255, such as 255 or a test killed by a signal, stops the bisect instead of marking the commit bad.
- Skipping next to the culprit. Git cannot tell which commit was first bad. It lists the candidates and prints
We cannot bisect more!. - Where it changed, not where the mistake is. The commit bisect names is where behavior changed. LWN noted in 2008 that the patch that makes a bug show up may not be the one that introduced it.
- No good commit, no bisect. Bisect only works if there was a point where the problem did not happen.
- Merges.
--first-parent(Git 2.29 and later) follows only the main line, so a bug that came in through a merged branch is pinned on the merge commit.
Keep commits small
Git's documentation advises one small logical change per commit and no commits that knowingly break things: the smaller the changes in a commit, the more effective git bisect is.
In this video
- Which commit broke it? About 10 checks
- How git bisect works: good, bad, halve
- Linus Torvalds's 2005 example, and 10,928 revisions
- git bisect run: your script answers with an exit code
- Exit 125: skip commits you can't test
- Best case: a kernel bisect in 20-30 minutes; not only bugs
- Catches: one wrong answer, unreliable tests, replay
- Skipped commits, and bugs that were always there
- What to do: start, bad, good, run, reset
- Keep commits small, and the verdict
Which commit broke it? About 10 checks 0:00
You asked for this one. Which of the last thousand commits broke your code? You'd assume you test them one by one. Git needs about ten checks. The command is git bisect. And yes, it's binary search: the manual says so itself.
How git bisect works: good, bad, halve 0:14
You mark one commit bad and an older one good. Git checks out one in the middle and asks: good, or bad? Each answer throws out about half the suspects. In the manual's example, 675 revisions left becomes 337.
Linus Torvalds's 2005 example, and 10,928 revisions 0:29
That example goes back to Linus Torvalds's original git bisect commit, in July 2005. And it scales. In Git's docs, a kernel search starts with nearly eleven thousand revisions left: roughly 14 steps.
git bisect run: your script answers with an exit code 0:43
Here's the surprise: you don't have to answer yourself. Git bisect run takes your test script, runs it at every step, and reads the exit code. Exit zero means good. Anything from 1 to 127 means bad, with one exception.
Exit 125: skip commits you can't test 0:59
Exit 125 means: I can't test this commit, skip it. Anything above 127 stops the search. So the manual's example script starts with make, or exit 125. A commit that won't build gets skipped, not blamed.
Best case: a kernel bisect in 20-30 minutes; not only bugs 1:15
As kernel developer Ingo Molnar put it, in a 2009 paper in Git's docs: "With Git bisect it's a breeze." His best case: a roughly 15-step kernel bisection in 20 to 30 minutes, automated. It isn't only for bugs. Pick your own terms, like fast and slow, and it finds the commit that made things slow.
Catches: one wrong answer, unreliable tests, replay 1:35
Now the catches. The Linux kernel's guide warns that one wrong answer sends the rest of the search "totally off course." Your test has to be reliable. Chasing a kernel that booted only sometimes, developer Nathan Chancellor passed a commit only when it booted ten times out of ten. Answered one wrong anyway? You don't have to start over: save git bisect log, remove the wrong entry, and replay it.
Skipped commits, and bugs that were always there 1:58
Skipping has a price too. Skip a commit right next to the culprit, and Git can't name one: you get a list of suspects. And the commit it names is where behavior changed, not always where the mistake was made. If the bug was always there, bisect can't help.
What to do: start, bad, good, run, reset 2:14
So here's what to do. Git bisect start, mark the bad commit and one that still worked, then git bisect run your test. When Git names the commit, git bisect reset puts you back where you started.
Keep commits small, and the verdict 2:26
And keep commits small: Git's docs say the smaller the change, the more effective bisect gets. So yes, it's binary search, but you don't do the searching. Give git bisect one good commit and a test you trust, and let it run.
What the sources say
- Git bisect uses a binary search to find the commit that introduced a change: it is given one bad commit and one earlier good commit, checks out a commit between them, and narrows the range with each good or bad answer. Git project
- The example in the Git manual prints 675 revisions left to test (roughly 10 steps) and, one answer later, 337 revisions left (roughly 9 steps). The count is what remains after the commit just checked out, so the full range in that example is about 1,350 commits. Git project
- The 675 and 337 figures in the manual come from the commit by Linus Torvalds that added the git bisect command on 30 July 2005, and its example is about the Linux kernel. [PATCH] Making it easier to find which change introduced a bug, Git - git-bisect Documentation
- In a paper that ships with Git's documentation, a bisection of the Linux kernel between v2.6.25 and v2.6.27 starts with 10,928 revisions left to test and an estimate of roughly 14 steps. Git project
- The git bisect run command automates the search with a script: exit code 0 marks a commit good, any code from 1 to 127 except 125 marks it bad, and any other exit code aborts the bisect. Git - git-bisect Documentation, Fighting regressions with git bisect
- Exit code 125 tells git bisect run that a commit cannot be tested, so that commit is skipped. The manual says 125 was chosen as the highest sensible value because POSIX shells already use 126 and 127. Git project
- Bisect is not only for bugs: the terms old and new can replace good and bad, and custom terms such as fast and slow let it find the commit that introduced a performance regression. Git project
- When a skipped commit sits next to the culprit, Git cannot tell which commit was the first bad one, and it lists the candidates instead of naming one. Git - git-bisect Documentation, Fighting regressions with git bisect
- The Linux kernel's bisection guide warns that getting a single good or bad answer wrong sends the rest of the bisection totally off course. The Linux Kernel documentation
- Linux kernel developer Ingo Molnar, quoted in a 2009 paper in Git's documentation, said an automated kernel bisection of about 15 steps takes him 20 to 30 minutes in the best case. Git project
Sources
- Git - git-bisect Documentation Git project
- git-scm.com
- Fighting regressions with git bisect Git project
- [PATCH] Making it easier to find which change introduced a bug Git project
- Bisecting a regression The Linux Kernel documentation
- nathanchance.dev
- lwn.net
- raw.githubusercontent.com
- raw.githubusercontent.com
More in Developer tools explained
-
2:51
Devcontainers Explained: One File for Your Team, and the AI Catch
Devcontainers explained: one devcontainer.json file sets up the same dev environment for your whole team, and here is the catch when you let an AI agent run…
-
2:51
Git Worktree Explained: Run Coding Agents in Parallel (and the Catch)
git worktree explained: one repository, several folders, each with its own branch checked out at once. We follow the example in git's own manual (an urgent fix…