日本語

How to find the commit that introduced a bug with git bisect

​​Git​command line,shell script

To find the cause of a regression, where something worked in an earlier version but broke at some point, use git bisect. Tell Git one commit that works (good) and one commit that is broken (bad). Git then checks out the commits between them one at a time, using a binary search. For each one, you test the behavior and answer good or bad. When it finishes, Git tells you the commit where the bug first appeared. If you put the test into a script, git bisect run can run the whole process automatically.

Note

The environment used for verification is Ubuntu 24.04 and Git 2.43.0. The commit author's name and email are sample values (Neko <neko@example.com>). To make the output the same every time, the commit dates are set with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. The target being tested is a small shell script. I did not test Windows or macOS, or usages that change the names of good and bad with options such as --term-old.

Example: a bug where an exact total of 3000 yen does not get free shipping

The example uses total.sh, which calculates a total from a unit price and a quantity. The rule is that orders of 3000 yen or more ship free, and anything cheaper gets a 500 yen shipping fee added.

console
$ git log --oneline
b14f6a7 バージョンを1.1にする
4955859 お知らせを更新
38b0dbb 送料の金額を変数にまとめる
44eb671 fi の書き忘れを直す
9053b97 送料無料の判定を整理する(途中)
029ce06 READMEに税込みの注記を追加
4875160 お知らせを追加
06fd303 READMEに使い方を追加
e691ad9 3000円以上は送料無料にする
7aca488 合計を計算するスクリプトを追加
4197c2e 最初のコミット

Two items at 1500 yen each come to exactly 3000 yen, so the correct behavior is free shipping and output of 3000. However, on the current main, the output is 3500. When you go back to the tag v1.0 (e691ad9), the output is the correct 3000.

console
$ ./total.sh 1500 2
3500

$ git checkout -q v1.0 && ./total.sh 1500 2; git checkout -q main
3000

So we know it broke somewhere between v1.0 and main. There are 8 commits after v1.0, counting main. Instead of going back and testing them one by one, let git bisect find the commit for us.

Start the search by giving a good and a bad commit

Begin with git bisect start, then tell Git that the current commit is bad and v1.0 is good. If you omit the commit in git bisect bad, it refers to the current commit (HEAD).

console
$ git bisect start
status: waiting for both good and bad commits

$ git bisect bad
status: waiting for good commit(s), bad commit known

$ git bisect good v1.0
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[9053b972415adeb6959b2e7e3686ef297a03f4ef] 送料無料の判定を整理する(途中)

Once you have both a good and a bad commit, Git checks out the commit roughly in the middle of the range (official git bisect reference). The Bisecting: line shows how many commits remain to test and roughly how many more steps are needed. At this point you are in a detached HEAD state, not on a branch.

console
$ git status
HEAD detached at 9053b97
You are currently bisecting, started from branch 'main'.
  (use "git bisect reset" to get back to the original branch)

nothing to commit, working tree clean

Skip commits you cannot test with git bisect skip

When I ran the calculation on the checked-out 9053b97, the script stopped with an error. This commit forgot the fi, so the script does not run at all.

console
$ ./total.sh 1500 2
./total.sh: 8: Syntax error: end of file unexpected (expecting "fi")

Because this doesn't tell you whether the shipping bug is present, you can't answer good or bad. In that case, skip the commit with git bisect skip. Git then picks a different nearby commit.

console
$ git bisect skip
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[487516072f4a38998cfb6e32a88aca5f1d5a0ef7] お知らせを追加

According to the official reference, if you skip a commit that is right next to the commit you are looking for, Git can no longer determine which of the skipped commit and its neighbor is the first bad one.

Answer good or bad each time you test

After that, keep testing the checked-out commit and report the result. If the output is 3000, answer good. If it is 3500, answer bad.

console
$ ./total.sh 1500 2
3000

$ git bisect good
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[44eb671007f284a5b6149faf52c10ed8d55a2454] fi の書き忘れを直す

$ ./total.sh 1500 2
3000

$ git bisect good
Bisecting: 1 revision left to test after this (roughly 1 step)
[38b0dbb09220e849c72061c6245893c0a69e8fd2] 送料の金額を変数にまとめる

$ ./total.sh 1500 2
3500

$ git bisect bad
38b0dbb09220e849c72061c6245893c0a69e8fd2 is the first bad commit
commit 38b0dbb09220e849c72061c6245893c0a69e8fd2
Author: Neko <neko@example.com>
Date:   Wed Nov 11 11:00:00 2026 +0900

    送料の金額を変数にまとめる

 total.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

The output is the first bad commit shows that 38b0dbb (Group shipping fee amounts into variables) is the commit where the bug first appeared. Counting the skipped commit, the search narrowed 8 commits down to one in 4 checks.

A diagram with the commits from v1.0 to main arranged vertically. The first one, 9053b97, was skipped. The second, 4875160, was answered good, the third, 44eb671, was good, and the fourth, 38b0dbb, was bad, making 38b0dbb the first bad commit. The labels in the image are in Japanese.
The commits git bisect checked out, and the order in which I answered

Looking at the diff of the commit that was found shows the cause.

console
$ git show 38b0dbb
commit 38b0dbb09220e849c72061c6245893c0a69e8fd2
Author: Neko <neko@example.com>
Date:   Wed Nov 11 11:00:00 2026 +0900

    送料の金額を変数にまとめる

diff --git a/total.sh b/total.sh
index f403100..df031f8 100755
--- a/total.sh
+++ b/total.sh
@@ -1,8 +1,10 @@
 #!/bin/sh
 # Usage: ./total.sh unit_price quantity
+free_from=3000
+shipping=500
 sub=$(($1 * $2))
-if [ "$sub" -ge 3000 ]; then
+if [ "$sub" -gt "$free_from" ]; then
   echo "$sub"
 else
-  echo $((sub + 500))
+  echo $((sub + shipping))
 fi

When the amounts were grouped into variables, the comparison changed from -ge (greater than or equal to) to -gt (greater than). As a result, only an order of exactly 3000 yen stopped getting free shipping.

When finished, return to the original branch with git bisect reset

You can review your answers so far with git bisect log.

console
$ git bisect log
git bisect start
# status: waiting for both good and bad commits
# bad: [b14f6a73df4ddca50ce4f46d8a322b046671e21e] Set version to 1.1
git bisect bad b14f6a73df4ddca50ce4f46d8a322b046671e21e
# status: waiting for good commit(s), bad commit known
# good: [e691ad91b3d44b5686d708304be8803bb7272068] Make shipping free for orders of 3000 yen or more
git bisect good e691ad91b3d44b5686d708304be8803bb7272068
# skip: [9053b972415adeb6959b2e7e3686ef297a03f4ef] Clean up free-shipping check (in progress)
git bisect skip 9053b972415adeb6959b2e7e3686ef297a03f4ef
# good: [487516072f4a38998cfb6e32a88aca5f1d5a0ef7] Add announcement
git bisect good 487516072f4a38998cfb6e32a88aca5f1d5a0ef7
# good: [44eb671007f284a5b6149faf52c10ed8d55a2454] Fix missing fi
git bisect good 44eb671007f284a5b6149faf52c10ed8d55a2454
# bad: [38b0dbb09220e849c72061c6245893c0a69e8fd2] Group shipping fee amounts into variables
git bisect bad 38b0dbb09220e849c72061c6245893c0a69e8fd2
# first bad commit: [38b0dbb09220e849c72061c6245893c0a69e8fd2] Group shipping fee amounts into variables

Even after the commit that introduced the bug has been found, the working tree stays at the commit under test. git bisect reset returns you to the branch you were on when you ran git bisect start.

console
$ git bisect reset
Previous HEAD position was 38b0dbb 送料の金額を変数にまとめる
Switched to branch 'main'

$ git status -sb
## main
Point

During the search, you are in a detached HEAD state with an old commit checked out. Make the commit that fixes the bug after you return to the original branch with git bisect reset.

Put the test into a script and let git bisect run do the work

If you can decide good or bad with a command, you can put that decision in a script and pass it to git bisect run. Git decides good or bad from the script's exit code (official git bisect reference).

Exit code Treatment
0 good
125 Cannot be tested, so skip
1–127 (except 125) bad
Anything else Abort the search

"Anything else" includes values 128 and above. According to the official reference, a program that ends with exit(-1) has the exit code 255, so it is treated as an abort rather than bad. For bad, end with 1.

Here, I wrote a script that marks a commit good if 1500 yen × 2 produces 3000. If total.sh itself stops with an error, the script exits with 125 so bisect skips the commit. The official reference's examples also recommend keeping the script outside the repository so that it does not interfere with the bisect process. Here it is placed at ../check.sh.

console
$ cat ../check.sh
#!/bin/sh
# 1500 yen times 2 pieces, and free shipping at 3000 yen makes it good
out=$(./total.sh 1500 2) || exit 125
[ "$out" = 3000 ]

The last line, [ "$out" = 3000 ], exits with 0 if the values match and with 1 if they don't.

console
$ [ "3000" = 3000 ]; echo "終了コード: $?"
終了コード: 0

$ [ "3500" = 3000 ]; echo "終了コード: $?"
終了コード: 1

git bisect start can take the bad and good commits in one go. git bisect start HEAD v1.0 combines the three commands from before: start, bad, and good v1.0.

console
$ git bisect start HEAD v1.0
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[9053b972415adeb6959b2e7e3686ef297a03f4ef] 送料無料の判定を整理する(途中)

$ git bisect run ../check.sh
running '../check.sh'
./total.sh: 8: Syntax error: end of file unexpected (expecting "fi")
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[487516072f4a38998cfb6e32a88aca5f1d5a0ef7] お知らせを追加
running '../check.sh'
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[44eb671007f284a5b6149faf52c10ed8d55a2454] fi の書き忘れを直す
running '../check.sh'
Bisecting: 1 revision left to test after this (roughly 1 step)
[38b0dbb09220e849c72061c6245893c0a69e8fd2] 送料の金額を変数にまとめる
running '../check.sh'
38b0dbb09220e849c72061c6245893c0a69e8fd2 is the first bad commit
commit 38b0dbb09220e849c72061c6245893c0a69e8fd2
Author: Neko <neko@example.com>
Date:   Wed Nov 11 11:00:00 2026 +0900

    送料の金額を変数にまとめる

 total.sh | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
bisect found first bad commit

The script exited with 125 at 9053b97, so that commit was skipped, just as when I skipped it by hand. After that, it checked out commits in the same order as my manual answers and arrived at the same 38b0dbb. git bisect run stops once it finds the first bad commit, so as with the manual search, run git bisect reset to return to the original state.

console
$ git bisect reset
Previous HEAD position was 38b0dbb 送料の金額を変数にまとめる
Switched to branch 'main'

$ git branch --show-current
main

​Posted by 86