CH 32 / 75Part 5: Practical Git Techniques

bisect: Binary Search for the Commit That Introduced a Bug

What you will learn on this page

  • Understand that bisect narrows the range between a "good" version and a "bad" version in half each step
  • Identify the culprit commit by answering with git bisect good / bad
  • Automate the checking with git bisect run
  • Return to where you were with git bisect reset when you're done
On this page10 items

In 31, we searched the history for a specific change using words and lines as clues. Sometimes, though, “something is wrong, but I can’t even tell which line is to blame.” That’s when bisect comes in: it mechanically finds the first commit where things went wrong.

Narrowing it down by halves#

Bisect means “to cut in two”. There’s only one prerequisite: you need a version you know was good and a version you know is bad. Git splits the commits between them in half, checks out the one in the middle, and asks “how about this one?” Each time you answer, the range to search is cut in half.

In the practice repository, we’ll add to a cake recipe little by little. Along the way, we accidentally write the amount of salt as “100g”.

text8 lines
cd ~/git-practice
echo "Sugar 80g" >> recipes/cake.txt && git commit -qam "Add sugar to the cake"
echo "Flour 200g" >> recipes/cake.txt && git commit -qam "Add flour to the cake"
echo "Butter 50g" >> recipes/cake.txt && git commit -qam "Add butter to the cake"
echo "Salt 100g" >> recipes/cake.txt && git commit -qam "Adjust the cake quantities"
echo "Milk 100ml" >> recipes/cake.txt && git commit -qam "Add milk to the cake"
echo "Vanilla a dash" >> recipes/cake.txt && git commit -qam "Add vanilla to the cake"
echo "Baking time 30 min" >> recipes/cake.txt && git commit -qam "Add the cake baking time"

-q suppresses the output shown after each commit. Since the tag v1.1 from 30, seven commits have been added.

text1 lines
git log --oneline v1.1..HEAD
text7 lines
4b2f2ca Add the cake baking time
aa185d8 Add vanilla to the cake
3d87e5c Add milk to the cake
fc9f50a Adjust the cake quantities
3fab8de Add butter to the cake
47b7712 Add flour to the cake
9e05ef9 Add sugar to the cake

The situation: “It tastes salty when baked. It was fine back at v1.1.” Looking at the messages, you can’t be sure “Adjust the quantities” is the culprit. Let’s find it with bisect.

Eight commits in a row from v1.1 (good) to the baking time (bad). The first check, at the butter commit, is good; the second, at the milk commit, is bad; the third, at the adjust-quantities commit, is bad. The range narrows by half each time, revealing that adjusting the quantities is the culprit commit
Each answer halves the range to search

Searching while checking by hand#

Starting#

text3 lines
git bisect start
git bisect bad
git bisect good v1.1
text4 lines
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)
[3fab8de2c077163861943e173211ad86961756fc] Add butter to the cake

bad on its own means “the commit I’m on now (HEAD) is bad”, and good v1.1 means “v1.1 was good”. With both in place, Git checked out “Add butter to the cake” in the middle and set the working tree to that point. roughly 2 steps is the estimate that “it’ll be done in about two more rounds”.

At this point, HEAD has left the branch and points directly at the checked-out commit (the detached HEAD from Part 3, #18).

Answering#

Check the recipe at the checked-out point.

text1 lines
cat recipes/cake.txt
text4 lines
Eggs 2
Sugar 80g
Flour 200g
Butter 50g

No salt yet. It’s correct, so answer good.

text1 lines
git bisect good
text2 lines
Bisecting: 1 revision left to test after this (roughly 1 step)
[3d87e5c9f85d35bc86e1d2daa48260fbd0393309] Add milk to the cake

It moved to the latter half of the range and checked out “Add milk”. cat recipes/cake.txt shows “Salt 100g”, so it’s bad.

text1 lines
git bisect bad
text2 lines
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[fc9f50a56b011e877850963392fa45dab62b59e3] Adjust the cake quantities

This one also has “Salt 100g”, so bad again.

text1 lines
git bisect bad
text9 lines
fc9f50a56b011e877850963392fa45dab62b59e3 is the first bad commit
commit fc9f50a56b011e877850963392fa45dab62b59e3
Author: Taro Yamada <[email protected]>
Date:   Thu Oct 22 10:00:00 2026 +0900

    Adjust the cake quantities

 recipes/cake.txt | 1 +
 1 file changed, 1 insertion(+)

“Adjust the cake quantities” was found as is the first bad commit. Seven commits, pinpointed with just three answers.

Finishing#

text1 lines
git bisect reset
text2 lines
Previous HEAD position was fc9f50a Adjust the cake quantities
Switched to branch 'main'

reset ends bisect and returns you to the branch you were on before starting (main).

Automating the check: git bisect run#

If you can decide good or bad with a command, as with “does Salt 100g appear?”, you can leave the answering itself to Git.

text2 lines
git bisect start HEAD v1.1
git bisect run sh -c '! grep -q "Salt 100g" recipes/cake.txt'
text7 lines
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[3fab8de2c077163861943e173211ad86961756fc] Add butter to the cake
running 'sh' '-c' ''\!' grep -q "Salt 100g" recipes/cake.txt'
(snip)
fc9f50a56b011e877850963392fa45dab62b59e3 is the first bad commit
(snip)
bisect found first bad commit

git bisect start <bad version> <good version> lets you specify start, bad and good on one line. The command after git bisect run is executed for each checked-out commit; success counts as good, failure as bad. Here we pass a command that “succeeds if Salt 100g isn’t found” (grep -q only reports whether it found a match, and the leading ! inverts that result).

For a program, the typical approach is to pass your test command as is. When it finishes, return with git bisect reset, just as with the manual method.

Once you’ve found the cause#

Once you know the culprit commit, it becomes clear what to fix. This time it’s a typo in the quantity, so correct it and commit.

text2 lines
sed -i.bak 's/Salt 100g/Salt a pinch/' recipes/cake.txt && rm recipes/cake.txt.bak
git commit -am "Fix the amount of salt in the cake"

If the culprit commit doesn’t contain other changes, you can also cancel it out with git revert from Part 2, #15.

FAQ#

Q. What if I can’t check a checked-out commit for some other reason?

A. You can skip that commit with git bisect skip. Git checks out another nearby commit instead.

Q. I don’t know which version was good.

A. Try an old tag or a commit from well back as good. Even with a wide range, halving means the number of rounds doesn’t grow much. Even 1,000 commits take only about 10 rounds.

Summary#

  • bisect narrows the range between a good version and a bad version in half each step, finding the first commit where things went wrong
  • Start with git bisect start → bad → good <good version>, check the checked-out point, and answer good or bad
  • If you can decide good or bad with a command, git bisect run automates the answers
  • When finished, git bisect reset returns you to the branch you were on before starting
  • The smaller your commits, the faster the cause you find tells you what to fix

Next page#

33. worktree: Opening Multiple Branches at Once ── Learn how to open another branch in a separate folder and work on it at the same time, without switching branches.