At the first release, the menu search box worked. Now, searching for "coffee" finds nothing. Between the first release and now there are nine commits (saved sets of changes in Git, the program that records changes to your code). Not one of those commits is called "break the search".
A feature that used to work, then broke after a change, is called a regression. The command git bisect finds the first commit that broke it. It does this by halving the commit history (the row of commits from old to new) again and again, and checking the commit in the middle with a test. Picture cooking a pot of soup and adding seasoning ten times. After each addition, you set a spoonful aside in a small glass. At the start it tastes right, at the end it's too salty. Once there's too much salt, it never tastes right again. Instead of tasting ten glasses in a row, you taste the middle glass first. If it still tastes right, the problem is in the glasses after it. Here, each glass is one commit, tasting is running the test, and soup that is too salty is a broken commit.
Regressions and the monotonic property
An online coffee shop has a menu search box. In release v1.0, searching for coffee finds "Coffee with Milk" and "Iced Coffee". Here is the commit history, oldest first, with the search test's result at each commit:
| index | commit | test searchMenu(menu, 'coffee') |
|---|---|---|
| 0 | release v1.0 | OK: 2 results |
| 1 to 6 | add tea menu, tidy up menu layout, add price to menu card, fix footer typo, add category filter, change cart icon | OK: 2 results |
| 7 | optimize search | FAIL: 0 results |
| 8 | add promo page | FAIL: 0 results |
| 9 | update README (HEAD) |
FAIL: 0 results |
HEAD in the last row is Git's name for the commit you are on, here the newest one. The pattern is OK OK OK OK OK OK OK FAIL FAIL FAIL. Once one commit is broken, every commit after it is broken too. This property is called monotonic. Halving only works because of this property: check one commit in the middle, and half the history can be crossed off at once. Bisect can point at the wrong commit if the test sometimes passes and sometimes fails on the same commit (flaky), or if the bug went away and came back.
Make a repeatable test first
Before you use bisect, you need a way to check "broken or not" at any commit. It must be quick, with the same result every time. For the coffee shop, the test is this test.js file (require loads searchMenu from app.js):
// test.js: check the menu search
const { searchMenu } = require('./app.js');
const results = searchMenu(['Coffee with Milk', 'Sweet Tea', 'Iced Coffee'], 'coffee');
if (results.length === 2) {
console.log('OK: 2 results');
process.exit(0); // 0 = good
}
console.log(`FAIL: ${results.length} results, should be 2`);
process.exit(1); // 1 to 127, except 125 = bad
process.exit(0)ends the program with exit code 0, which means this commit is good.process.exit(1)means it's broken. Git calls these two results good and bad.- While you bisect, keep
test.jsout of the commits (untracked: not recorded by Git). Bisect moves to old commits, and iftest.jswere committed later, the old commits wouldn't have the file.
Let Git do the search: git bisect run
The git bisect command is explained fully in the Git Bisect lesson of the Git learning path. For a regression with an automated test, three lines are enough:
git bisect start HEAD v1.0 # HEAD is broken, v1.0 is still good
git bisect run node test.js # Git checks out, tests and marks on its own
git bisect reset # back to the branch you started on
v1.0is a tag (a name attached to a commit), here the first release's commit. The branch in the last line is the line of commits you are working on.git bisect runrunsnode test.jsat every commit it picks. Before that, Git checks out the commit: your project folder now holds what that commit holds. Exit code0means good,125is skipped, any other from1to127means bad, and any other exit code stops the bisect.- At the end Git prints
<hash> is the first bad commit, followed by the details of the "optimize search" commit.<hash>is that commit's unique code. That commit is the suspect. - A commit that can't be tested at all (for example, its build is broken) is skipped. Use
git bisect skipwhen you bisect by hand (marking each commit yourself), orexit 125from the script. bisect runis most useful when the test is fast and deterministic (always the same result).
Tracing findFirstBad (what the exercise does)
In the exercise, commits are just index numbers, and isBad(commit) is the test: it gives true when that commit is broken. You write a binary search (a search that halves the list each time), the method bisect uses:
function findFirstBad(commits, isBad) {
let low = 0;
let high = commits.length - 1;
let answer = -1; // no broken commit found yet
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (isBad(commits[mid])) {
answer = mid; // candidate: the first broken one is here or to its left
high = mid - 1;
} else {
low = mid + 1; // mid is good, so everything to its left is good too
}
}
return answer;
}
function countCalls(commits, isBad) {
let calls = 0;
const wrapped = (c) => { calls++; return isBad(c); };
const firstBad = findFirstBad(commits, wrapped);
return { firstBad, calls };
}
const commits10 = Array.from({ length: 10 }, (_, i) => i);
const r1 = countCalls(commits10, (c) => c >= 7);
console.log(r1.firstBad); // 7
console.log(r1.calls <= 5); // true
Before you look at the trace, guess: for 10 commits (commits10, index 0 to 9) with the bug starting at index 7 (c >= 7), how many times is isBad called? 4, 7, or 10?
Here is the trace of each round of the loop:
| Step | low | high | mid | isBad(mid) |
Then |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | false | low = 5 |
| 2 | 5 | 9 | 7 | true | answer = 7, high = 6 |
| 3 | 5 | 6 | 5 | false | low = 6 |
| 4 | 6 | 6 | 6 | false | low = 7, the loop ends |
Only 4 calls, and the result is 7.
answer = midis saved beforehighmoves left, because the broken commit atmidmight be the first one.- For 1,000 commits with the bug starting at 742, the result is 742 after 10 calls. So in the exercise,
r2.calls <= 11istrue. - If nothing is broken,
answeris never set, so the result is-1. If everything is broken, the search keeps moving left, so the result is0. countCallswrapsisBadwith a counter, then runsfindFirstBadwith that wrapper.
Git itself needs fewer tests than findFirstBad, because you already told it both ends: v1.0 is good and HEAD is broken.
💡 Once the commit is found
git show <hash> shows everything that commit changed. In the "optimize search" commit, toLowerCase() (which turns text into lowercase) turns out to have been removed. So 'Coffee with Milk'.includes('coffee') is false. Fix it, then add the check from test.js to your project's test suite (its collection of tests) as a regression test. So CI fails right away if this bug ever comes back.
⚠️ What makes bisect (and the exercise) miss
- A flaky test. Random results break the monotonic property, and then bisect points at the wrong commit with no error at all. Make the test stable first.
- Marking one commit wrong. Bisect uses every good or bad mark you give, as it is. One
badthat should have beengoodmakes bisect point at an innocent commit. - In the exercise: forgetting to save the answer. If
answer = midisn't written beforehigh = mid - 1, the first broken commit gets thrown away. And when nothing is broken, the result must be-1, notlow. - In the exercise: checking one by one. A loop from the front does find 742, but needs 743 calls. The exercise checks
calls <= 11for 1,000 commits, and at most 7 calls for 64 commits. Both checks pass only if you halve.
TL;DR: A regression is a feature that used to work, then broke after a change. git bisect finds the first commit (a saved set of changes in Git) that broke it, by halving the commit history. This works because of the monotonic property: once one commit is broken, every commit after it is broken too. Prepare a fast, deterministic test (always the same result). Its exit code: 0 = good, 1 to 127 except 125 = bad (broken), 125 = skip. Then run git bisect start HEAD v1.0 (HEAD is the newest commit, which is broken; v1.0 is still good), git bisect run node test.js and git bisect reset. A commit that can't be tested is skipped with git bisect skip. In the exercise, findFirstBad is a binary search (a search by halving). If isBad(mid) (the test for the middle commit) is true, save answer = mid and move left. If it is false, move right. 10 commits with the bug starting at index 7 take only 4 isBad calls, and 1,000 commits only 10. The result is -1 if nothing is broken, and 0 if everything is broken. Once the commit is found, fix it and add a regression test.
⚡ Regressions and the monotonic property
Meaning: A regression is a feature that used to work, then broke after a change. The command git bisect finds the first commit (a saved set of changes in Git) that broke it, by halving the commit history. This works because of the monotonic property: once one commit is broken, every commit after it is broken too.
⚡ A repeatable test
Write:
if (results.length === 2) {
console.log('OK: 2 results');
process.exit(0); // 0 = good
}
console.log(`FAIL: ${results.length} results, should be 2`);
process.exit(1); // 1 to 127, except 125 = bad
Meaning: This is the test for one commit: searching the menu for "coffee" must find 2 results. The exit code tells Git: 0 is good, 1 to 127 except 125 is bad, 125 is skipped. The test must be fast and give the same result every time.
⚡ Let Git do the search
Write:
git bisect start HEAD v1.0 # HEAD is broken, v1.0 is still good
git bisect run node test.js # Git checks out, tests and marks on its own
git bisect reset # back to the branch you started on
Meaning: Here HEAD is the newest commit, and v1.0 is the release commit. Checking out means Git moves to a commit, and a branch is a line of commits. At the end Git prints <hash> is the first bad commit (<hash> is the commit's unique code). That commit is the suspect.
⚡ Keep halving
Guess first: isBad is the test for one commit. There are 10 commits, and the bug starts at index 7. If you always check the commit in the middle, how many times is isBad called?
Only 4. Check the middle commit: if it's broken, the first broken one is there or to its left (an older commit). If it's good, everything to its left is good too. 1,000 commits take only 10 calls.
Meaning: This way of searching by halving is called binary search, the algorithm git bisect uses.