git bisect for Regressions — Debugging

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…

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

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

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.

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

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.

What you will learn