More Git answers

by Danny
Published 2 min read

git bisect runs a binary search over your history to find the exact commit that broke something. Mark one good and one bad commit, test each step, or automate it with bisect run.

The short answer

#

Use git bisect. You tell Git one commit where the bug exists and one where it did not, and Git checks out the commit halfway between. You test it, mark it good or bad, and repeat until Git names the first bad commit.


_10
git bisect start
_10
git bisect bad # the current commit has the bug
_10
git bisect good v2.3.0 # this tag did not
_10
# test, then run git bisect good or git bisect bad, and repeat
_10
git bisect reset # go back to where you started

Why it happens

#

Bugs often appear without anyone noticing which change caused them. By the time someone reports it, there may be hundreds of commits since the last known good version, and reading them all is slow.

Bisect is a binary search. Each test halves the range of suspect commits, so 1,000 commits take about 10 tests, and 100 take about 7. Once you have the offending commit, its diff and message usually make the cause obvious, which is far quicker than debugging the whole app from scratch.

How to fix it

#

Bisect by hand

Pick a good commit you are confident about, such as a release tag. Then let Git guide you.


_10
git bisect start
_10
git bisect bad HEAD
_10
git bisect good v2.3.0
_10
# Bisecting: 52 revisions left to test after this (roughly 6 steps)

Git now has a commit checked out. Build or run the app, check for the bug, and report back.


_10
git bisect good # the bug is not here
_10
git bisect bad # the bug is here
_10
git bisect skip # this commit cannot be tested, for example it does not build

When it finishes, Git prints the first bad commit. Run git bisect reset to return to your original branch. If you lose track, git bisect log shows every answer you gave so far.

You can pass both ends to start in one go, bad first then good:


_10
git bisect start HEAD v2.3.0

Automate it with git bisect run

If you can check for the bug with a command, Git can do the whole search for you. The command's exit code is the answer: 0 means good, 125 means skip, and any other code from 1 to 127 means bad.


_10
git bisect start HEAD v2.3.0
_10
git bisect run npm test -- --run src/cart.test.ts

For anything more involved, write a small script. Make sure it exits with 125 when the commit cannot be tested, so a broken build is not mistaken for the bug.


_10
#!/usr/bin/env bash
_10
# check-bug.sh
_10
npm ci --silent || exit 125
_10
npm run build --silent || exit 125
_10
node scripts/reproduce-bug.js


_10
chmod +x check-bug.sh
_10
git bisect run ./check-bug.sh

Keep the script outside the tracked files, or untracked, so checking out older commits does not change or remove it.

Use clearer words than good and bad

Sometimes you are hunting for when something was fixed or when behaviour changed, and "good" and "bad" read backwards. You can choose your own terms.


_10
git bisect start --term-old=slow --term-new=fast
_10
git bisect slow v2.3.0
_10
git bisect fast HEAD

old and new also work out of the box: git bisect old and git bisect new.

Reduce noise from merged branches

In a history with many merges, bisect can step into feature branches. If you only care about which merge into main brought the bug in, follow the first parent only.


_10
git bisect start --first-parent

Common pitfalls

#
  • A "good" commit that was not really good. If your starting good commit already had the bug, bisect will give a wrong answer. Test it first.
  • Uncommitted changes. Bisect checks out other commits, so commit or stash your work before you start.
  • Stale dependencies. Older commits may need different packages. Reinstall in your test script, or you may test old code against new dependencies.
  • Treating build failures as bad. Use git bisect skip by hand, or exit 125 in scripts, for commits that cannot be tested.
  • Forgetting to reset. Until you run git bisect reset, you are on a detached HEAD in the middle of a search.
  • Flaky tests. An intermittent failure gives random answers. Run the check several times in your script before deciding.
#

Was this resource helpful?