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).
| Commits to search | Reading one by one, worst case | Halving |
|---|---|---|
| 24 | 24 | 5 |
| 100 | 100 | 7 |
| 1,000 | 1,000 | 10 |
| 10,000 | 10,000 | 14 |
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
f14f4f2main Give a frying time for lumpia4cdbdacWelcome correctionsa2c062aMore soy sauce in the pancitf130ee5Steam the leche flan a little longerd9c81bcAdd a bay leaf to the adobo3590c5cGive a starting amount of fish sauce338b504Bind the lumpia with two eggsb4ecbf5Add a carrot to the pancita5e3d67Tidy the adobo ingredient list4d63136Say which sugar for the caramel1fe589bExplain one dish per filec3f6063HEAD Use three tomatoes in the sinigang
and 12 older commits
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.
- Start with
git bisect start - Mark HEAD bad, and an old commit good (
git bisect good HEAD~23) - Judge each commit Git checks out until it names the first bad one
- 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
f14f4f2HEAD →main Give a frying time for lumpia4cdbdacWelcome correctionsa2c062aMore soy sauce in the pancitf130ee5Steam the leche flan a little longerd9c81bcAdd a bay leaf to the adobo3590c5cGive a starting amount of fish sauce338b504Bind the lumpia with two eggsb4ecbf5Add a carrot to the pancita5e3d67Tidy the adobo ingredient list4d63136Say which sugar for the caramel1fe589bExplain one dish per filec3f6063Use three tomatoes in the sinigang0dd8ec1Say the lumpia count is roughbe76b30Name the noodles in the pancit67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe
and 8 older commits
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:
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 saltin seconds.
Spot the trap: commits that don't work at all
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
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.
- Git documentation: git-bisect: start, good, bad, skip, run, log and reset
- Pro Git: Debugging with Git: binary search, and bisect run