How Git works · lesson 4 of 6
Commands and ideas in this lesson
Agit bisect startbegin a search
Bgit bisect good, badjudge the commit Git checked out
Cgit bisect runlet a command judge each commit
Dgit bisect resetfinish and go back

Finding the commit that broke it

Last updated

Lola Remy tastes the adobo from Kusina's latest recipe and pulls a face: far too salty. Somewhere in the last 24 commits, something changed. You could read them all. Or you could let Git find it in five tries with git bisect, which is what this lesson is about.

By the end of this lesson you can

  • Explain bisect as a binary search through history
  • Find a bad commit with git bisect start, good, bad and reset
  • Automate the search with git bisect run
  • Explain why small commits that each work make bisect useful

Halve it, and halve it again

You know the newest commit is bad (salty) and an old one is good. Somewhere between them is the first bad commit. Test the commit in the middle:

  • If it's good, the problem came later: forget the older half.
  • If it's bad, the problem is in it or before it: forget the newer half.

Each test halves what's left. That's binary search, the same idea as guessing a number between 1 and 100 by asking "higher or lower?" (Simple DSA covers it in Stage 3).

How many tests to find one commit
Commits to searchReading one by one, worst caseHalving
24245
1001007
1,0001,00010
10,00010,00014

git bisect does the bookkeeping: it checks out each middle commit for you, and remembers what you've said.

Watch a bisect

$ 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 HEAD~23
Bisecting: 11 revisions left to test after this (roughly 4 steps)
[c3f60634ffb22fce43642d52cdf6f94df6952fab] Use three tomatoes in the sinigang
  1. f14f4f2 main Give a frying time for lumpia
  2. 4cdbdac Welcome corrections
  3. a2c062a More soy sauce in the pancit
  4. f130ee5 Steam the leche flan a little longer
  5. d9c81bc Add a bay leaf to the adobo
  6. 3590c5c Give a starting amount of fish sauce
  7. 338b504 Bind the lumpia with two eggs
  8. b4ecbf5 Add a carrot to the pancit
  9. a5e3d67 Tidy the adobo ingredient list
  10. 4d63136 Say which sugar for the caramel
  11. 1fe589b Explain one dish per file
  12. c3f6063 HEAD Use three tomatoes in the sinigang

and 12 older commits

1 of 6 Start, and tell Git that HEAD is bad and the very first commit, 23 commits back, was good.

Find it yourself

The same repository. Judge each commit Git checks out by looking at the salt line in adobo.md (grep salt adobo.md, or open the file): 1 tsp is good, 1 tbsp is bad.

Git sandbox
  1. Start with git bisect start
  2. Mark HEAD bad, and an old commit good (git bisect good HEAD~23)
  3. Judge each commit Git checks out until it names the first bad one
  4. End the search with git bisect reset
You are in ~/kusina. Somewhere in the last 24 commits, the adobo became far too salty.

Commit graph

  1. f14f4f2 HEAD →main Give a frying time for lumpia
  2. 4cdbdac Welcome corrections
  3. a2c062a More soy sauce in the pancit
  4. f130ee5 Steam the leche flan a little longer
  5. d9c81bc Add a bay leaf to the adobo
  6. 3590c5c Give a starting amount of fish sauce
  7. 338b504 Bind the lumpia with two eggs
  8. b4ecbf5 Add a carrot to the pancit
  9. a5e3d67 Tidy the adobo ingredient list
  10. 4d63136 Say which sugar for the caramel
  11. 1fe589b Explain one dish per file
  12. c3f6063 Use three tomatoes in the sinigang
  13. 0dd8ec1 Say the lumpia count is rough
  14. be76b30 Name the noodles in the pancit
  15. 67e435d Cover the leche flan with foil
  16. ba04ca2 Add lumpiang shanghai recipe

and 8 older commits

Which commit made the adobo salty?

If you mark one wrongly, git bisect log shows what you said so far, and git bisect reset lets you start again.

Let a command decide: bisect run

If a command can tell good from bad, Git can run it on each commit for you. git bisect run <command> treats exit code 0 as good and any other (except 125, which means "skip") as bad. Here grep -q succeeds only while the old line is there:

Terminal
git bisect start HEAD HEAD~23
git bisect run grep -q "1 tsp salt" adobo.md
running 'grep' '-q' '1 tsp salt' 'adobo.md'
...
a5e3d67554cada4a3aafefe68206bee0a34372a2 is the first 'bad' commit
...
bisect found first 'bad' commit
git bisect reset

In a real project the command is usually the test suite, or a small test you've just written for this bug. Try it in the sandbox above: git bisect reset first, then run these. Note that the test file must not be committed in the history you're searching, or it would come and go as Git checks out commits; keep it outside the repository, or untracked.

What makes bisect work

Bisect names a commit. How useful that is depends on the commits:

  • Small commits (lesson 1.7): "Tidy the adobo ingredient list" changed one line, so the cause is obvious. A commit called "Lots of changes" that touched 30 files would only narrow it to 30 files.
  • Every commit works. Bisect checks out commits in the middle of history. If many of them don't run at all, you can't judge them.
  • Honest messages. The message said "tidy", which hid a real change. A message that said "Change salt to 1 tbsp" would have been found by git log --grep salt in seconds.

Spot the trap: commits that don't work at all

Spot the trapJoaquin

Joaquin bisects a bug in build_index.py. The commit Git checks out crashes before it gets anywhere near the bug: it’s from a day when the script was half-written. He marks it bad, because it crashed.

What happens if…

Check what you learned

About how many tests does bisect need to search 1,000 commits?
In git bisect run, what does exit code 0 mean?
Why do small commits make bisect more useful?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git