DEV Community

Cover image for Git Bisect Explained: Find the Bad Commit in About 10 Checks
kush_exe
kush_exe

Posted on Originally published at kuzscotech.github.io

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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?
Enter fullscreen mode Exit fullscreen mode
git bisect start HEAD HEAD~10 --   # culprit is among the last 10
git bisect run ~/test.sh
git bisect reset                   # quit the bisect session
Enter fullscreen mode Exit fullscreen mode

The same thing on one line, with no script file:

git bisect run sh -c "make || exit 125; ~/check_test_case.sh"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Which commit broke it? About 10 checks

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

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

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

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

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

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

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

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

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

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.

Sources

Read the full transcript, with every source, on the page for this video. More explainers like this one are on the Kush_EXE YouTube channel.

This article is built from the video's script and published sources. The video uses AI illustrations.

Top comments (0)