Cheat sheet for the video [Git Bisect Explained: Find the Bad Commit in About 10 Checks](https://www.youtube.com/watch?v=VscF36YrMNs) · [read the full page](https://kuzscotech.github.io/kush-exe/git-bisect-explained/)

## 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](https://git-scm.com/docs/git-bisect).

## 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](https://git-scm.com/docs/git-bisect-lk2009) |

The count is what is left after the commit just checked out.

## Catches

- **One wrong answer.** The [Linux kernel's guide](https://docs.kernel.org/admin-guide/bug-bisect.html) 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](https://nathanchance.dev/posts/working-with-git-bisect/) 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](https://lwn.net/Articles/277872/) 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.
