Finding a bug with bisect
Binary-search your history to find the commit that introduced a bug, first by hand and then automatically with git bisect run and a test script.
Reading history with log and diffThe reflog and recovering lost work
Finding a bug with bisect
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `git bisect` binary-searches the commits between a known good one and a known bad one, so 1,000 commits need only about 10 tests.
- You start with `git bisect start`, mark one `bad` and one `good` commit, then mark each commit Git checks out until it names the first bad commit.
- `git bisect run <script>` automates the loop, treating exit code 0 as good and 1 to 127 (except 125) as bad.
- A test that doesn't actually detect the bug makes bisect blame the wrong commit, so confirm the script fails on the bad commit and passes on the good one first.
- Always finish with `git bisect reset` to return to the branch you started from.
When you know it worked once
jot add used to keep every note. Now it keeps only the last one, and you have 11 commits since the version you released as v1. Reading all the diffs is tedious. But you know something useful: version one was good and today is bad. That is enough to search.
Bisecting is binary search over history. Check the commit in the middle. If it’s bad, the bug is in the first half, and if it’s good, it’s in the second. Each test halves the suspects, so 1,000 commits take about ten tests, not a thousand.
A first attempt that goes wrong
I made a repository with 11 commits. Commit 7 changed jot.sh to overwrite notes.txt instead of appending, and the rest only changed the README. The tag v1 is the first commit. My first test script added one note and checked that it showed up as number 1:
#!/bin/sh
rm -f notes.txt
./jot.sh add "buy milk"
./jot.sh list | grep -q "1.*buy milk"
Then I bisected by hand and by script, and the answer was:
b3b37dfb97bc... is the first bad commit
Docs change 11
README.md | 1 +
That is a docs-only commit. Bisect pointed with confidence at the wrong commit, and the reason is my script: overwriting and appending look identical when you add just one note. The script passed on every commit, so Git decided the very last one was the first bad. Garbage test, garbage answer. Bisect trusts you completely.
So the rule: run your test on a commit you know is bad and one you know is good before you start.
A script that detects the bug
Two notes distinguish the behaviours:
#!/bin/sh
rm -f notes.txt
./jot.sh add "buy milk"
./jot.sh add "call bank"
test "$(./jot.sh list | wc -l)" -eq 2
$ ./check.sh; echo $? # on HEAD
1
$ git checkout -q v1
$ ./check.sh; echo $?
0
Fails on the bad end, passes on the good end. Now the search can be trusted. (Keep the script outside the repository, or commit it where every commit you test has it. Otherwise old commits won’t contain it.)
By hand
$ git bisect start
$ git bisect bad
$ git bisect good v1
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[98fb3d4f...] Docs change 6
Git checked out the middle. You test it however you like, then report:
$ ./check.sh && git bisect good || git bisect bad
Each report prints the next commit, until: <hash> is the first bad commit. git bisect log lists what you reported, which you can save and replay. Hashes will differ for you.
Automatically
When you have a script, let Git drive:
$ git bisect start HEAD v1
$ git bisect run ./check.sh
running './check.sh'
Bisecting: 2 revisions left to test after this (roughly 1 step)
...
d4295324c441... is the first bad commit
Speed up add
jot.sh | 2 +-
bisect found first bad commit
Three tests found it among ten candidates. The culprit is “Speed up add”, the one-character change from >> to >. bisect run reads the exit code: 0 is good, 1 to 127 is bad (except 125). Use exit 125 in a script to say “I can’t test this commit”, for example when it doesn’t compile. You can also mark a commit by hand with git bisect skip.
Finish with:
$ git bisect reset
Switched to branch 'main'
Until you reset, you’re on a detached HEAD partway through history. Any change you make goes on that detached HEAD.
When bisect works best
- The history has small, buildable commits, which is another argument for good commit messages and clean history.
- You can write a command that returns pass or fail, even a crude
grep. - The bug is deterministic. A flaky test makes bisect lie.
Once bisect names the commit, the question becomes “what was that change for?”. That is blame and searching history.
What the video covers
anthonywritescode’s “using git bisect to find a bug in nodejs (intermediate) anthony explains #569” is about sixteen and a half minutes and was published around 2024. From its title it applies bisect to a real bug in a large project, which is a fuller treatment than the short intros. I judge English from the title, description and channel only, and I could not read a transcript. I can’t confirm whether it uses git bisect run, so the script-driven run and the broken-test pitfall above are from my own runs. Two shorter introductions are linked.
Where this fits
Bisect finds the commit. Next you’ll look at the lines inside it: blame and searching history.
Check yourself
About how many tests does bisect need for 1,000 commits?
About 10, since each test halves the range. 1,000 commits is just under 2 to the power of 10.
What does the exit code of a `git bisect run` script mean?
0 marks the commit good, and 1 to 127 except 125 marks it bad. 125 means skip this commit because it can't be tested.
Why must you run your test script on both a known good and a known bad commit before bisecting?
If it passes on the bad commit or fails on the good one, bisect will point at a wrong commit with full confidence.
What does `git bisect reset` do?
It ends the bisect session and checks out the branch you were on before, instead of leaving you on a detached HEAD in the middle of history.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.