TExcelTAD
Home › Git › Git Bisect

Git Bisect

Something worked last month and is broken today, with 200 commits in between. Which one broke it? git bisect finds it by binary search: it checks out the commit in the middle, you say "good" or "bad", and it halves the range each time. 200 commits need only about 8 tests (2⁸ = 256).

The manual workflow

bash
git bisect start
git bisect bad                 # the current commit is broken
git bisect good v1.2           # this older version worked
# Git checks out a commit in the middle: test it, then
git bisect good                # or: git bisect bad
# ... repeat until Git prints "<hash> is the first bad commit"
git bisect reset               # return to where you started

A bisect session

Here a repository has 8 commits; one of them breaks a price calculation. Each step marks the checked-out commit by running the check by hand:

bash · try it yourself
mkdir shop && cd shop && git init -q
echo 'price() { echo $(( $1 * 120 / 100 )); }' > price.sh
git add . && git commit -q -m "Add price with 20% tax"
git tag v1.0
for n in 2 3 4 5 6 7 8; do
  echo "# note $n" >> notes.txt
  if [ "$n" = 5 ]; then sed -i 's/120/102/' price.sh; fi    # the bug sneaks in
  git add . && git commit -q -m "Change $n"
done
check() { source ./price.sh; [ "$(price 100)" = 120 ]; }

git bisect start
git bisect bad HEAD
git bisect good v1.0 | head -1
for step in 1 2 3; do
  if check; then result=good; else result=bad; fi
  echo "testing $(git log -1 --format=%s): $result"
  out=$(git bisect $result); echo "${out%%
  

\n'*}"
done
git bisect reset >/dev/null 2>&1
status: waiting for both good and bad commits
status: waiting for good commit(s), bad commit known
Bisecting: 3 revisions left to test after this (roughly 2 steps)
testing Change 4: good
Bisecting: 1 revision left to test after this (roughly 1 step)
testing Change 6: bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
testing Change 5: bad
12a3210ece1951c5877939164835f40b4e15774f is the first bad commit

Three tests were enough to pinpoint "Change 5" among 7 candidates.

Automating it: git bisect run

If a script can decide good/bad (exit status 0 = good, 1–124 = bad, 125 = skip this commit), Git runs the whole search for you:

bash · try it yourself
mkdir shop && cd shop && git init -q
echo 'price() { echo $(( $1 * 120 / 100 )); }' > price.sh
git add . && git commit -q -m "Add price with 20% tax"
git tag v1.0
for n in 2 3 4 5 6 7 8; do
  echo "# note $n" >> notes.txt
  if [ "$n" = 5 ]; then sed -i 's/120/102/' price.sh; fi
  git add . && git commit -q -m "Change $n"
done
cat > ~/test-price.sh <<'EOF'
source ./price.sh
[ "$(price 100)" = 120 ]
EOF

git bisect start HEAD v1.0 >/dev/null
git bisect run bash ~/test-price.sh | grep -E "is the first bad commit|^    "
git bisect log | grep -E "^# (good|bad|first)"
git bisect reset >/dev/null 2>&1
12a3210ece1951c5877939164835f40b4e15774f is the first bad commit
    Change 5
# bad: [c401152278f9796584b165029d09732e896999f1] Change 8
# good: [d26f51c17e6a6ef0c990e25654bc29eb3e50aeb5] Add price with 20% tax
# good: [1a8d02461350475327e9a01225d7e7b40cd0189b] Change 4
# bad: [0d17293998601b5cb90536926cffdcd95d000c66] Change 6
# bad: [12a3210ece1951c5877939164835f40b4e15774f] Change 5
# first bad commit: [12a3210ece1951c5877939164835f40b4e15774f] Change 5

git bisect start HEAD v1.0 is a shortcut for start + bad + good. The test script lives outside the repository (~/test-price.sh), so it exists in every checked-out commit.

Tips

  • Use git bisect skip for commits that can't be tested (for example, they don't build).
  • git bisect visualize (or git bisect view) shows the remaining suspects.
  • git bisect log prints the session so you can replay it with git bisect replay.
  • Small commits make bisect much more useful: the "first bad commit" is then a tiny, obvious diff.
  • If the words good/bad are confusing (e.g. searching for when something got fixed), use git bisect start --term-old=slow --term-new=fast.

Test yourself

About how many tests does bisect need to search 1000 commits?

  1. 1000
  2. 500
  3. About 10
  4. 2

In git bisect run ./test.sh, what should the script return for a good commit?

  1. Exit status 0
  2. Exit status 1
  3. Exit status 125
  4. It should print GOOD

What does git bisect reset do?

  1. Deletes the bad commit
  2. Ends the session and returns to the branch you started from
  3. Marks all commits as good
  4. Restarts the search