Skip to main content

Dev tools and workflow · Intermediate · 16 min

Find the commit that broke something with git bisect

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Git 2.56.1 (git-core PPA)

You will search a 200-commit history for the one commit that broke a test, first by hand and then with a script that lets Git do the search on its own, and fix the bug it finds.

On this page

A test passed last month. Today it fails, and 200 commits landed in between. Reading every diff would take an afternoon. git bisect does a binary search through the history instead: you tell it one commit where things worked and one where they do not, it checks out the commit halfway between, you say whether that one is good or bad, and it halves the range again. Two hundred commits take about eight answers. With a small test script, Git gives those answers itself.

In this tutorial you generate a practice repository, ~/bisect-demo/shop, with 200 commits. Its price.sh adds 7.5% VAT to a price in cents, and its test.sh checks that 1000 cents comes out as 1075. Somewhere in the history the sum broke, and one commit in the middle does not even parse. You find the culprit by hand, then hand the search to git bisect run, save and replay the session, use your own words instead of "good" and "bad" to find a slowdown, and finally fix the bug.

Everything here is local. Once the repository is on your machine, bisect needs no network, so a long search costs no data on a prepaid plan.

Step 1 · Install Git 2.56

Ubuntu 26.04 ships Git 2.53, which already has every bisect feature in this tutorial. If you want the current release, 2.56.1 at the time of writing, the Git maintainers publish it in the ppa:git-core/ppa package repository. Add the repository and upgrade Git:

Bash
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt install -y git
git --version

Packages from the PPA arrive through sudo apt upgrade like any other, so you get later Git releases the same way.

Check it worked

The last command prints git version 2.56.1 or a later 2.56 patch. If it still says 2.53, run apt policy git and check that the PPA's version is listed as the candidate.

Step 2 · Generate a history with a hidden bug

This script builds the repository. Each of its 200 commits adds a line to notes.txt, and a few of them change the code: one introduces the bug, two in a row leave price.sh half-edited and unparseable, and one makes a report script slow. Save it and run it:

Bash
mkdir -p ~/bisect-demo
cd ~/bisect-demo
cat > make-history.sh <<'SCRIPT'
#!/bin/sh
# Builds ./shop: a Git repository with 200 commits and one hidden bug.
set -eu
mkdir shop
cd shop
git init --quiet --initial-branch=main

write_price() {
  # $1 is ok, bug or broken
  case "$1" in
    ok)     vat='vat=$(( net * 75 / 1000 ))' ;;
    bug)    vat='vat=$(( net * 75 / 100 ))' ;;
    broken) vat='if [ -n "$net" ]; then vat=$(( net * 75 / 1000 ))' ;;
  esac
  cat > price.sh <<EOF
#!/bin/sh
# Print a price in cents with 7.5% VAT added, rounded down.
net="\$1"
$vat
echo \$(( net + vat ))
EOF
}

write_report() {
  # $1 is fast or slow
  {
    echo '#!/bin/sh'
    [ "$1" = slow ] && echo 'sleep 2 # look up every order in the archive'
    echo 'echo "Orders today: 42"'
  } > report.sh
}

cat > test.sh <<'EOF'
#!/bin/sh
# 1000 cents plus 7.5% VAT is 1075 cents.
got=$(sh price.sh 1000)
if [ "$got" = "1075" ]; then
  echo "PASS: price.sh 1000 = $got"
else
  echo "FAIL: price.sh 1000 = $got, expected 1075"
  exit 1
fi
EOF

price=ok
report=fast
for i in $(seq 1 200); do
  msg="Update the notes"
  case "$i" in
    64)  report=slow;  msg="Read order totals from the archive" ;;
    125) price=broken; msg="Start reworking the VAT check" ;;
    126) price=ok;     msg="Finish reworking the VAT check" ;;
    137) price=bug;    msg="Tidy the VAT sum" ;;
    *)   [ $((i % 3)) -eq 0 ] && msg="Reword the help text" ;;
  esac
  write_price "$price"
  write_report "$report"
  echo "change $i" >> notes.txt
  git add .
  git commit --quiet -m "Change $i: $msg"
done
echo "Made $(git rev-list --count HEAD) commits in $(pwd)"
SCRIPT
sh make-history.sh
cd ~/bisect-demo/shop

The script prints Made 200 commits in followed by the repository's path. Every commit message starts with Change <number>, so you can tell where you are in the history at a glance. In a real project you would not know where the bug is; here you can read the script to check your answer at the end, but try not to look yet.

Now see the failure on the latest commit:

Bash
sh test.sh

It prints FAIL: price.sh 1000 = 1750, expected 1075. That is the "bad" end of the search.

Check it worked

Find the very first commit, which has no parent, keep its hash in a shell variable, and look at it:

Bash
first=$(git rev-list --max-parents=0 HEAD)
git log --oneline -1 "$first"
git rev-list --count HEAD

You see Change 1: Update the notes and the count 200. The variable lasts as long as this terminal. If you open a new one, cd ~/bisect-demo/shop and run the first= line again.

Step 3 · Mark the first commits by hand

Start a bisect session, mark the current commit (HEAD, the newest) as bad and the first commit as good:

Bash
git bisect start
git bisect bad
git bisect good "$first"

Git answers with something like Bisecting: 99 revisions left to test after this (roughly 7 steps) and checks out Change 100: Update the notes. You are now on a detached HEAD in the middle of the history. Test it:

Bash
sh test.sh

It prints PASS, so the bug arrived later. Tell Git:

Bash
git bisect good

Git jumps to Change 150: Reword the help text, halfway through what is left. Test again:

Bash
sh test.sh

This one prints FAIL, so the bug is at Change 150 or before it:

Bash
git bisect bad

Check it worked

Bash
git bisect log

The log lists two bad and two good lines, after the git bisect start line and some status comments, with full hashes and commit messages, the last of them bad: [...] Change 150: Reword the help text. Git now checks out Change 125: Start reworking the VAT check, and only about 25 commits are left in the range.

Step 4 · Skip a commit you cannot test

Run the test on Change 125:

Bash
sh test.sh

The output starts with a syntax error and then a FAIL line. On Ubuntu, where sh is dash, it reads price.sh: 6: Syntax error: end of file unexpected (expecting "fi"); bash words it differently. That is not the bug you are looking for: the file does not parse at all, so this commit tells you nothing about the VAT sum. Ask the shell to check the syntax without running anything:

Bash
sh -n price.sh

It reports the same syntax error. Marking this commit bad would send the search the wrong way. Skip it instead, and Git picks a nearby commit to test:

Bash
git bisect skip
git log --oneline -1

In a real project the same thing happens with commits that do not compile, need a database migration you do not have, or fail for an unrelated reason. Skip them. If the culprit sits right next to skipped commits, Git may finish with a short list of "possible first bad commits" instead of one, and you check those few by eye.

Check it worked

The log line shows a commit near the one you skipped, something like Change 123: Reword the help text or Change 127: Update the notes. Git chooses it pseudo-randomly from the commit hashes, which depend on when the commits were made, so your number may differ. It is never Change 125 again.

Step 5 · Let a script do the rest with git bisect run

Typing good and bad gets tedious, and people make mistakes. git bisect run runs a command on each commit and reads its exit code: 0 means good, 125 means skip, any other code from 1 to 127 means bad, and 128 or above stops the whole search.

Write a check script that turns "does not parse" into 125 and otherwise runs the test. Keep it outside the repository, in ~/bisect-demo, so that checking out old commits never changes or removes it:

Bash
cat > ~/bisect-demo/check.sh <<'EOF'
#!/bin/sh
# Exit 0 = good, 1 = bad, 125 = this commit cannot be tested.
if ! sh -n price.sh 2>/dev/null; then
  echo "SKIP: price.sh does not parse"
  exit 125
fi
sh test.sh
EOF

You are still in the middle of the session from the last two steps, with your hand-made answers kept. Hand it over:

Bash
git bisect run sh ~/bisect-demo/check.sh

Git prints a running 'sh' line with the path to your script before each test, then the PASS or FAIL line, then where it goes next. After about five more tests it stops with:

Text
<hash> is the first 'bad' commit
commit <hash>
...
    Change 137: Tidy the VAT sum

 notes.txt | 1 +
 price.sh  | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

followed by bisect found first 'bad' commit. Git 2.56 puts the term in quotes; other releases may print it without them.

Check it worked

While the session is still open, Git keeps the answer in the ref refs/bisect/bad. Save its hash for the fix later, then end the session:

Bash
culprit=$(git rev-parse refs/bisect/bad)
git log --oneline -1 "$culprit"
git bisect reset
git status

The first line shows Change 137: Tidy the VAT sum. git bisect reset puts you back on main, and the status says On branch main with a clean working tree.

Step 6 · Run the whole search unattended, then replay it

Now do it from scratch, with no hand marking at all. git bisect start accepts the bad commit first and then the good one, so one line sets up the range:

Bash
git bisect start main "$first"
git bisect run sh ~/bisect-demo/check.sh
git bisect log > ~/bisect-demo/vat-bug.log
git bisect reset

Count the running lines in the output: about eight tests for 200 commits, one of them the unparseable Change 125, which the script skipped by itself. The answer is the same commit.

The saved log is a plain text record of every answer. Open ~/bisect-demo/vat-bug.log in an editor: each git bisect good, bad or skip line follows a comment naming the commit. You can attach it to a bug report so a colleague sees exactly what you tested. You can also replay it, which rebuilds the session without running any tests:

Bash
git bisect replay ~/bisect-demo/vat-bug.log
git bisect reset

Replay is also how you recover from a wrong answer during a hand search. Save the log, delete the line where you marked a commit wrongly and everything after it, replay the edited file, and carry on from there.

Check it worked

The replay ends with the same is the first 'bad' commit message for Change 137: Tidy the VAT sum, and git bisect reset returns you to main.

Step 7 · Search for a slowdown with your own terms

Not every regression is a bug. Here, report.sh used to finish at once and now takes two seconds. Calling the fast commits "good" and the slow ones "bad" works, but it gets confusing when you hunt for the commit that made something faster, where the newer state is the one you want. --term-old and --term-new let you name the two states.

Write a check that times the script. timeout 1 stops report.sh after one second and returns exit code 124, so anything slower counts as slow:

Bash
cat > ~/bisect-demo/is-fast.sh <<'EOF'
#!/bin/sh
# Exit 0 if report.sh finishes within one second, 1 if it is slower.
if timeout 1 sh report.sh >/dev/null; then
  echo "FAST"
else
  echo "SLOW"
  exit 1
fi
EOF

On macOS, timeout is not installed by default. Homebrew's coreutils package provides it as gtimeout.

Start a session with the new words. Git now accepts git bisect slow and git bisect fast in place of bad and good, and in git bisect run exit 0 means the old state, fast:

Bash
git bisect start --term-old=fast --term-new=slow
git bisect slow
git bisect fast "$first"
git bisect terms
git bisect run sh ~/bisect-demo/is-fast.sh

git bisect terms prints two lines:

Text
Your current terms are 'fast' for the old state
and 'slow' for the new state.

The search takes about a second for each slow commit it tests and ends with <hash> is the first 'slow' commit.

Check it worked

Bash
git log --oneline -1 refs/bisect/slow
git bisect reset

The log shows Change 64: Read order totals from the archive. With custom terms the answer lives in refs/bisect/slow instead of refs/bisect/bad.

Step 8 · Read the culprit and fix it

Bisect tells you where to look. Now look. Show just the code change in the culprit commit:

Bash
git show "$culprit" -- price.sh

The diff is one line: vat=$(( net * 75 / 1000 )) became vat=$(( net * 75 / 100 )), which is 75% VAT instead of 7.5%. A "tidy" that dropped a zero.

No later commit touched price.sh, so the version from the commit just before the culprit is the correct one. Restore that file, run the test and commit the fix with the culprit's hash in the message, so the history explains itself:

Bash
git restore --source="$culprit~1" price.sh
sh test.sh
git commit -am "Fix VAT: divide by 1000 again (broken in $(git rev-parse --short "$culprit"))"

If later commits had changed the same file, restoring the whole file would undo their work too. Then you would edit the one line by hand, or try git revert "$culprit", and resolve any conflict: see undo mistakes in Git.

Check it worked

Bash
sh test.sh
git log --oneline -2

The test prints PASS: price.sh 1000 = 1075, and the newest commit is your fix, with Change 200: Update the notes below it.

Step 9 · Clean up

When you are done, remove the practice files. This deletes the repository and the scripts, and leaves Git and the PPA installed:

Bash
cd ~
rm -rf ~/bisect-demo

Check it worked

Bash
ls ~/bisect-demo 2>/dev/null || echo "bisect-demo removed"

The command prints bisect-demo removed.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.