git bisect answers the question every developer eventually asks: this worked last month, it is broken now, which commit broke it? Instead of reading hundreds of diffs, you tell Git one commit where the bug exists and one where it does not. Git checks out the commit halfway between them, you say whether it is good or bad, and the range halves. For 100 commits that is about 7 tests, for 1,000 about 10. With git bisect run and a small test script, Git runs every step for you and prints the culprit. This tutorial builds a practice repository with a hidden bug so you can follow along, then covers automation, skipping untestable commits, custom terms for performance regressions and the mistakes that point bisect at the wrong commit. Everything was run with Git 2.50.

git bisect binary search: mark a known good and a known bad commit, git tests the middle commit and halves the range until the first bad commit is found
git bisect performs a binary search between a known good commit and a known bad commit.

From my own work: The regressions where bisect saves the most time are the boring ones: a rounding change, a dependency bump, a config default that changed in a refactor. They are hard to spot in review because each diff looks harmless, and easy to find with bisect because the test is a single assertion.

Build a practice repository

This script creates a repository with 100 commits. Commit 1 has a correct discount function; one commit in the middle replaces rounding with integer division, which drops the cents on some prices. Run it in an empty directory:

#!/usr/bin/env bash
# make-demo.sh - build a practice repository with 100 commits and one hidden bug
set -euo pipefail
mkdir bisect-demo && cd bisect-demo
git init -q -b main
git config user.name "Demo" && git config user.email "demo@example.com"

cat > pricing.py <<'PY'
def apply_discount(price_cents, percent):
    """Return the price after a percentage discount, rounded to whole cents."""
    return round(price_cents * (100 - percent) / 100)
PY
echo "release 1" > CHANGELOG.md
git add . && git commit -qm "release 1: discount helper"
git tag v1.0

for i in $(seq 2 100); do
  echo "release $i" >> CHANGELOG.md
  if [ "$i" -eq 64 ]; then
    # the regression: integer division drops the cents
    sed -i.bak 's|round(price_cents * (100 - percent) / 100)|price_cents * (100 - percent) // 100|' pricing.py
    rm -f pricing.py.bak
    git commit -qam "refactor pricing: use integer math"
  else
    git commit -qam "release $i: update changelog"
  fi
done
git log --oneline | wc -l

The script uses GNU and BSD compatible sed -i.bak, so it works on Linux and macOS. Next, a test that passes on good commits and fails on bad ones. A 70 percent discount on 19.99 should cost 6.00; the broken version returns 5.99:

#!/bin/sh
# check_discount.sh - exit 0 if the commit is good, 1 if it is bad
python3 -c '
from pricing import apply_discount
assert apply_discount(1999, 70) == 600, apply_discount(1999, 70)
'

Keep the test script outside the repository, here one directory up. During a bisect Git checks out old commits, and a script inside the working tree would change or disappear with them.

A manual git bisect session

A session always has the same shape: start, mark one bad and one good commit, then test and mark until Git names the first bad commit. All subcommands are in the git-bisect documentation:

cd bisect-demo
git bisect start
git bisect bad              # the current commit (HEAD) has the bug
git bisect good v1.0        # this tag did not
# Bisecting: 49 revisions left to test after this (roughly 6 steps)
# [c1936f2d] release 50: update changelog

../check_discount.sh && echo good || echo bad   # test this commit
git bisect good             # passes here
# Bisecting: 24 revisions left to test after this (roughly 5 steps)
# [f7bc3eb2] release 75: update changelog

# ... keep testing and marking until Git prints "is the first bad commit"

git bisect reset            # always finish here: back to the branch you started on

Your test does not have to be a script. It can be reloading a page, running a CLI command or clicking through a form; the only requirement is that you can decide good or bad for each commit Git checks out. If you are unsure about a commit, for example because it does not build, run git bisect skip and Git picks a nearby commit instead.

The shortcut git bisect start BAD GOOD does the first three commands in one, for example git bisect start main v1.0. You can also pass several good commits if the bug might have arrived through different branches.

Automate it with git bisect run

When the test is a command, let Git drive. git bisect run checks out each candidate, runs the command and uses its exit status to mark the commit:

git bisect start main v1.0
git bisect run ../check_discount.sh
running '../check_discount.sh'
Bisecting: 24 revisions left to test after this (roughly 5 steps)
[f7bc3eb28ab1311592732ff681ce2a7947659d63] release 75: update changelog
running '../check_discount.sh'
AssertionError: 599
Bisecting: 12 revisions left to test after this (roughly 4 steps)
[ac6275462bf97399727f2f15feaef37e98e0040c] release 62: update changelog
...
b7f4e9b6fcb646a2e9750211669a189c0d4ba13d is the first bad commit
commit b7f4e9b6fcb646a2e9750211669a189c0d4ba13d
Author: Demo <demo@example.com>

    refactor pricing: use integer math

 CHANGELOG.md | 1 +
 pricing.py   | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
bisect found first bad commit

Seven test runs found the one bad commit among 100. Then run git show b7f4e9b to read the change, and git bisect reset to return to main. Your hashes will differ because they include the commit dates.

For a one-off check you do not need a script file. Any command works, including an inline Python expression whose exit status is non-zero when the bug is present:

git bisect start main v1.0
git bisect run sh -c 'python3 -c "from pricing import apply_discount as a; raise SystemExit(a(1999, 70) != 600)"'
# b7f4e9b6fcb646a2e9750211669a189c0d4ba13d is the first bad commit

In real projects the command is usually your existing test runner limited to the relevant test, such as git bisect run npm test -- pricing or a single pytest file. If the test you need was written after the bug appeared, copy it outside the repository first, so every old commit runs the same test.

Exit codes: good, bad, skip and abort

git bisect run reads the exit status of your command:

Exit status Meaning for bisect Use it when
0 Good: the bug is not present The test passed
1 to 127, except 125 Bad: the bug is present The test failed
125 Skip: this commit cannot be tested The code does not build, dependencies are missing, an unrelated bug blocks the test
128 to 255 Abort the whole bisect Something is wrong with the test environment itself

The difference between bad and cannot test matters. If a commit fails to build and your script returns 1, bisect treats the build failure as the bug and can name the wrong commit. A robust script checks preconditions first and returns 125 for them:

#!/usr/bin/env bash
# bisect-test.sh - exit codes for git bisect run:
#   0 = good, 1..124 and 126..127 = bad, 125 = cannot test (skip), 128+ = abort the bisect
set -uo pipefail

# 1. a commit that cannot be built or imported is not evidence either way: skip it
if ! python3 -c 'import pricing' 2> /dev/null; then
  exit 125
fi
# simulate a broken build on one commit, for the demo only
if [ "$(tail -n 1 CHANGELOG.md)" = "release 62" ]; then
  echo "build broken on this commit, skipping"
  exit 125
fi

# 2. the actual check; any failure means bad
python3 -c '
from pricing import apply_discount
assert apply_discount(1999, 70) == 600
' 2> /dev/null || exit 1
$ git bisect start main v1.0 && git bisect run ../bisect-test.sh
...
[ac6275462bf97399727f2f15feaef37e98e0040c] release 62: update changelog
build broken on this commit, skipping
Bisecting: 12 revisions left to test after this (roughly 4 steps)
[a1bfe56d376781859c6f977856f679c48e834cf6] release 60: update changelog
...
b7f4e9b6fcb646a2e9750211669a189c0d4ba13d is the first bad commit

Bisect simply moved to a neighbouring commit. If the skipped commits sit right next to the culprit, Git cannot decide between them and lists all the candidates instead of one; that is still a much shorter list to read by hand. Note that the script deliberately does not use set -e: it decides the exit status explicitly, so an unexpected status above 127 cannot abort the bisect by accident.

Custom terms: performance regressions and finding fixes

Good and bad are awkward when you are not looking for a bug. To find when a page got slow, or when a bug was fixed so you can backport the fix, use your own terms:

# when did it get slow?
git bisect start --term-old=fast --term-new=slow
git bisect slow main
git bisect fast v1.0
# ... then mark commits with "git bisect fast" or "git bisect slow"

# when was it fixed? the old state is "broken", the new state is "fixed"
git bisect start --term-old=broken --term-new=fixed
git bisect terms            # prints the terms in use for this session

Git also has built-in aliases old and new for the same purpose. With git bisect run, exit status 0 still means the old state and 1 to 127 the new state. For performance checks, make the script compare against a threshold with some headroom, and run the measurement a few times, because noisy timings are the main reason performance bisects land on the wrong commit. A budget check like the ones in bundle size budgets in CI or Lighthouse CI JSON report analysis makes a good bisect script, because it already returns a clear pass or fail.

Narrowing the search: paths and first-parent

Limit to paths. If you know the bug lives in certain files, pass them after --. Bisect then only considers commits that touched those paths:

git bisect start main v1.0 -- pricing.py
# [b7f4e9b6] refactor pricing: use integer math    <- the only commit that touched the file

This is fast but only safe when the bug cannot come from elsewhere, such as a changed caller, a dependency or a config default.

Follow only the main line. On histories with many merged feature branches, the commits inside those branches may never have been deployed and may not even build. git bisect start --first-parent follows only the first parent of merge commits, so the search moves across the merges on your main branch and names the merge that brought the bug in. You can then bisect inside that branch if needed.

Logs, replays and seeing what is left

A long manual session is easy to mis-mark. Save the session, fix the mistake and replay it, as described in the Pro Git chapter on debugging:

git bisect log > bisect.log      # every decision so far, as replayable commands
git bisect visualize --oneline   # the commits still in range (alias: git bisect view)
git bisect reset

# edit bisect.log, delete the wrong "git bisect good/bad" line and everything after it
git bisect replay bisect.log
# bad: [cb26b525bfa492374d2cf0ec384f0163286ac29b] release 100: update changelog
# good: [6a696d5337066949ad7795f72c0383ed1c795804] release 1: discount helper
git bisect start 'main' 'v1.0'
# good: [c1936f2dd924f5f33ae41544eb978b372c88d1bd] release 50: update changelog
git bisect good c1936f2dd924f5f33ae41544eb978b372c88d1bd
# bad: [f7bc3eb28ab1311592732ff681ce2a7947659d63] release 75: update changelog
git bisect bad f7bc3eb28ab1311592732ff681ce2a7947659d63
...
# first bad commit: [b7f4e9b6fcb646a2e9750211669a189c0d4ba13d] refactor pricing: use integer math

Attaching the log to a bug report also lets a colleague reproduce the search exactly.

Mistakes that send bisect to the wrong commit

  • A good commit that was not really good. Verify the bug is absent on the commit you mark good before you start. If it was already there, bisect still returns a commit, just a wrong one.
  • Uncommitted changes and build artefacts. Start from a clean working tree. Compiled files, caches and node_modules from a newer commit can make old commits behave like new ones; have the script rebuild or clean what it needs.
  • Flaky tests. One random failure marks a good commit bad and the result is meaningless. Run the check several times in the script, or fix the flakiness first; finding flaky tests from JUnit XML in CI shows how to spot them.
  • Environment drift. Old commits may need old dependency versions or database schemas. Install dependencies from each commit’s lock file inside the script, and return 125 when setup fails.
  • Forgetting git bisect reset. Until you reset, HEAD stays detached on an old commit, and new commits made there are easy to lose.

After you find the commit, write the failing check as a permanent test and add it to CI, so the same regression cannot come back unnoticed; deploying with GitHub Actions shows a pipeline where such a test fits before the deploy step.

For a deeper look at how the algorithm chooses each commit and how it was used to hunt regressions in the Linux kernel, read Fighting regressions with git bisect.

git bisect checklist

  • A reproducible check that decides good or bad in under a minute.
  • The check verified on one known good and one known bad commit first.
  • Clean working tree; test script stored outside the repository.
  • git bisect start BAD GOOD, then git bisect run when the check is a command.
  • Exit 125 for untestable commits; 0 for good; 1 for bad.
  • --term-old and --term-new for performance or fix hunts.
  • -- path or --first-parent to narrow the search when safe.
  • git bisect log saved; git bisect reset at the end.
  • The check turned into a regression test in CI.

Want this done on your own server or site?

I am Saizul Amin, a web developer and technical SEO consultant. I set up and harden VPS servers, migrate WordPress sites, fix indexing problems and build automations. See my portfolio or get in touch.

FAQ

What does git bisect do?

git bisect performs a binary search through your commit history to find the commit that introduced a change, usually a bug. You mark one commit as bad and an older one as good, Git checks out the commit in the middle, and each answer halves the remaining range until one commit is left.

How many steps does git bisect take?

About log2 of the number of commits in the range: roughly 7 steps for 100 commits, 10 for 1,000 and 14 for 10,000. Skipped commits can add a few extra steps.

How do I automate git bisect?

Start the session with git bisect start BAD GOOD, then run git bisect run followed by a command or script. Git checks out each candidate and runs it: exit status 0 marks the commit good, 1 to 127 except 125 marks it bad, and 125 skips it.

What is the difference between git bisect skip and marking a commit bad?

skip tells Git the commit cannot be tested, for example because it does not build, so Git tries a nearby commit instead. Marking it bad tells Git the bug is present there, which moves the search and can produce a wrong result if the failure was unrelated.

How do I stop a git bisect session?

Run git bisect reset. It ends the session and checks out the branch or commit you were on when you started. git bisect reset HEAD instead stays on the commit currently checked out.

Sources

NEW BLOGS. REAL STRATEGIES. REAL RESULTS.

Join our community and receive powerful SEO tips, web optimization guides, and growth strategies as soon as they’re published.

Don’t just read blogs — stay ahead of the competition.

We don’t spam! Read our privacy policy for more info.

Share.

Comments are closed.