Your motorbike was in the parking lot at 08:00, and at 16:00 it was gone. You don't need to watch eight hours of CCTV footage. Play the 12:00 recording. Is it still there? Then it was taken between 12:00 and 16:00, and you never need to watch the morning footage again. Next, play the middle of the footage that is left, and repeat until you find the minute.
git bisect finds the first commit that brought in a bug (a mistake in the code) the same way. Your commit history is the footage: Git splits it in half again and again, and you check the commit in the middle. At the start, you only tell it which commit is still good (the bug isn't there yet) and which one is already broken.
The case: the cart total went wrong
An online shop has a total() function (the code that adds up the prices in the cart) in the file app.js. In release v1.0, a cart of $50 + $100 totals $150. Now, 8 commits later, the total is $135 for a regular buyer (not a member). Nobody remembers which commit caused it. Look at the history first with git log --oneline:
$ git log --oneline
c4b7f08 (HEAD -> main) add contact page
3e2d9c7 update README
8a6e1b6 add cart icon
5f0c8a5 apply member discount
9d3b7e4 tidy cart CSS
2c8f6d3 add buy button
7e1a5c2 fix footer typo
4b9d2f1 add product page
1a7c3e0 (tag: v1.0) release v1.0
The top commit (HEAD) is broken, and the bottom one (v1.0) is still good. Above v1.0 there are 8 candidates (commits that might have brought the bug in): 7 untested commits, plus HEAD, which is known to be broken. To test, there's a file test.js that checks the total. node test.js runs that file with Node (a program that runs JavaScript), and it prints OK when the total is right or FAIL when it's wrong.
The bisect steps
Here is one whole bisect session. Real hashes are 40 characters long, and here they are cut to 7 so they fit. Some lines of Git's answers are also left out.
$ git bisect start
$ git bisect bad # HEAD is broken now
$ git bisect good v1.0 # v1.0 still works
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[9d3b7e4] tidy cart CSS
$ node test.js
OK: total 150
$ git bisect good
Bisecting: 1 revision left to test after this (roughly 1 step)
[8a6e1b6] add cart icon
$ node test.js
FAIL: total 135, expected 150
$ git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[5f0c8a5] apply member discount
$ node test.js
FAIL: total 135, expected 150
$ git bisect bad
5f0c8a5 is the first bad commit
commit 5f0c8a5
Author: Bima <[email protected]>
apply member discount
$ git bisect reset
Previous HEAD position was 5f0c8a5 apply member discount
Switched to branch 'main'
git bisect startbegins a bisect session.git bisect badwith no hash means the commit you are on (HEAD) is broken.git bisect good v1.0means the commit taggedv1.0still works. You can also use a hash fromgit log --oneline, for examplegit bisect good 1a7c3e0.- Once Git knows one good commit and one broken commit, it moves (checks out) to the commit in the middle of the two. The files in your working folder are then replaced with that commit's files. In Git's answers, a revision means a commit.
3 revisions left to test after this (roughly 2 steps)means that after this commit there are still 3 commits that may need testing, about 2 more tests. - You test that commit with
node test.js, then answergit bisect good(it works) orgit bisect bad(it's broken). Each answer drops half the candidates, and Git moves to the middle of the candidates that are left. 5f0c8a5 is the first bad commitmeans5f0c8a5is the first broken commit, the one that brought the bug in. Its message, "apply member discount", tells you which part of the code to check.git bisect resetends the session and takes you back to the branch you were on.
During bisect, HEAD points straight at an old commit instead of at a branch. This is called a detached HEAD. That's why you must run git bisect reset at the end, so that HEAD goes back to your branch.
Why it is so fast
Every test drops half the candidates. In the session above, 8 candidates became 4, then 2, then 1. Only 3 tests, where checking one by one could take 8.
Now guess: with 1,024 candidate commits, at most how many tests? 10, 100, or 512?
| Candidates | One by one, at most | Bisect, at most |
|---|---|---|
| 8 | 8 tests | 3 tests |
| 100 | 100 tests | 7 tests |
| 1,024 | 1,024 tests | 10 tests |
| 1,000,000 | 1,000,000 tests | 20 tests |
1,024 halved ten times: 512, 256, 128, 64, 32, 16, 8, 4, 2, 1. So it takes only about 10 tests. It works the same way as binary search (searching an ordered list by splitting it in half). If the candidates double, you need just one more test.
💡 Let Git do the testing: git bisect run
If the test can run on its own, Git can mark each commit good or bad by itself. The test must be a script (a file of commands that runs as a program), like test.js. When a program ends, it reports a number called its exit code. The script must also end with exit code 0 when the commit is good and any other exit code when it's broken. Here is test.js from the example:
// test.js: check the cart total for a regular buyer
const { total } = require('./app.js')
const result = total([50, 100], { member: false })
if (result === 150) {
console.log('OK: total 150')
process.exit(0) // 0 = good
}
console.log(`FAIL: total ${result}, expected 150`)
process.exit(1) // anything but 0 = bad
Then run these two commands:
$ git bisect start HEAD v1.0 # bad and good in one go
$ git bisect run node test.js
git bisect start HEAD v1.0is the same asstart, thenbadforHEAD, thengood v1.0. The broken commit comes first.git bisect run node test.jsruns the test on each commit it checks out, then marks that commit itself. Exit code0becomes good,125means the commit is skipped, and any other code from1to127becomes bad.- A commit that can't be tested at all (for example, it fails to build: it can't be made ready to run) can be skipped with
git bisect skip. Git then moves to another commit nearby.
⚠️ When bisect points at the wrong commit
- Marking one commit wrong. Bisect trusts your answers completely. One
goodthat should have beenbadmakes Git point at the wrong commit, and no error appears. Test every commit before you answer. - Forgetting
git bisect reset. You stay on an old commit with a detached HEAD. New commits you make there don't land onmain. - The test script disappears. If
test.jswas committed only recently, older commits don't have that file, so it disappears from the folder when Git moves to them. Keep the test script out of the commits (untracked) while you bisect, so that it stays. - In the exercise, the test is
sh check.sh, notnode test.js. The exercise terminal has no Node, socheck.shis what checksapp.jsand printsOKorFAIL. That file isn't committed, so it stays there in every commit that gets checked out, just as the point before says.
TL;DR: git bisect finds the first broken commit, the one that brought in a bug (a mistake in the code), by splitting the history in half again and again. The order: git bisect start, git bisect bad (HEAD is broken), git bisect good v1.0 (the commit tagged v1.0 still works). Git then moves (checks out) to a commit in the middle. Test that commit (check whether the bug is there) and answer good or bad until is the first bad commit appears. Finish with git bisect reset. Each test drops half the candidates (the commits that might have brought in the bug), so 1,024 commits take only about 10 tests. git bisect run tests automatically with a test script (a program file that runs the test): exit code (the number a script reports when it ends) 0 means good.
⚡ Tell it the good end and the broken end
Write:
git bisect start
git bisect bad
git bisect good v1.0
Meaning: git bisect finds the first commit that brought in a bug (a mistake in the code). start begins the search. bad with no hash means the commit you are on (HEAD) is broken, and good v1.0 means the commit tagged v1.0 still works. Git moves straight to a commit in the middle of the two.
⚡ Test, then answer
Meaning: Test the commit Git moved to (check whether the bug is there), then answer git bisect good or git bisect bad. Each answer drops half the candidates (the commits that might have brought in the bug), until Git writes is the first bad commit. Finish with git bisect reset to get back to your branch.
In the exercise, the test is sh check.sh, which prints OK or FAIL. The lesson's example uses node test.js, but the exercise terminal has no Node (a program that runs JavaScript).
⚡ Why it's so fast
Guess first: each test drops half the candidates. With 1,024 candidate commits, at most how many tests?
About 10 tests. Halve 1,024 ten times and you're down to 1.
Meaning: It works like binary search (searching an ordered list by splitting it in half). If the candidates double, it takes just one more test.