Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Find the First Bad Commit

In this lesson, you let git halve the history for you. A 120-line order went through at release v1.0 but is declined at v1.5, with twelve commits in between and no file to start from.

Key Points

  • A check that decides good or bad for exactly one behavior. You save it outside the repo, in your checks folder. Committed to the repo, it would be missing at older commits. It builds its own data, so nothing needs seeding between commits. Git counts any error as bad, so an older createOrder() needing a field this toy state lacks would throw too and mislead the search. Such a commit should be skipped. The exercise shows how your check tells git.
  • Check both ends first. git checkout v1.0 swaps in that release’s files (git warns that you’re not on a branch, which is fine here), and the check passes. At v1.5, it fails. Then git switch main takes you back to where you started. A release you remember as good may not be. A search is only as trustworthy as its two ends.
  • git bisect start <bad> <good> sets up the search, bad commit first, and checks out the commit halfway between. git bisect run <command> runs your check at each stop.

Git ignores what the check prints. An uncaught error makes Node report a failure, which means bad, and a clean end means good.

Bisect assumes the behavior changed once, so everything before a good commit is good.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In Halve the Search, you saw that halving the history beats checking every commit. Now git does it for you. Say a 120-line order went through at release v1.0, but at v1.5 it’s declined.

00:15 After v1.0 came twelve commits. Which one changed it? With no file to start from, a file’s log can’t help.

00:25 First, you need a check that decides good or bad for exactly this behavior. Save it as basket-limit.mjs in your checks folder, outside the repo.

00:36 This one’s a toy. It builds its own state object, with 120 made-up books, so nothing needs seeding between commits. The basket has one line per book. It prints what you expect, then passes the order to createOrder().

00:51 If the order goes through, it prints accepted. If not, createOrder() throws an error and the script fails.

00:59 The top lines are the same setup as your shelf-count check. They load createOrder() from the folder you run it in, so it tests whichever commit is checked out.

01:08 Watch out: git counts any error as bad. An older createOrder() needing a field this toy state lacks would throw too, and mislead the search. Such a commit should be skipped.

01:20 The exercise shows how your check tells git.

01:25 Before you search, check both ends. git checkout v1.0 swaps your files for release v1.0’s files. Git warns that you’re not on a branch, which is fine here.

01:37 Run the check from the repo folder. The order is accepted, so v1.0 is good.

01:44 Check out v1.5 and run it. Now the error says a basket can have at most 100 book lines, so v1.5 is bad.

01:54 Then switch back to main, where you started. A release you remember as good may not be. If v1.0 had failed too, every answer after it would be wrong.

02:05 A search is only as trustworthy as its two ends.

02:10 git bisect start takes the bad commit, then the good one, and checks out the commit halfway between. git bisect run takes the command that runs your check.

02:21 Git ignores what the check prints. An uncaught error makes Node report a failure, which means bad. A clean end means good. The files change at each stop, while the check stays put.

02:34 Committed to the repo, it would be missing at older commits. The first stop is good. The order goes through. Bisect assumes the behavior changed once, so everything before a good commit is good, and that half drops out.

02:49 Release v1.4 is bad. Git halves again. The commit before it is bad too, with the same 100-line error, so this decline caused both. Release v1.3 is good.

03:04 So git names Ada’s commit, Decline oversized baskets before reserving stock. That took four runs for twelve commits.

03:15 The named commit is where the symptom first appeared. Is it where the cause was written? Read its diff. git show on that commit shows one added line, in orderLines().

03:26 It throws an error for a basket of more than 100 lines. That’s the decline itself, so this commit wrote the cause.

03:35 Sometimes, though, the named commit only sends new values into older code that was already wrong. So read the diff before you revert it. The message points to pull request 22.

03:48 There, Grace asks to decline a mistyped import of thousands of lines before reserving books. It’s not a bug, so changing the limit is Grace’s call. git bisect reset ends the search and takes you back to main.

04:04 Here, searching for the error text would have found that line too. Bisect works without a message. It needs only a check. Here’s the rule: start from two ends you’ve checked, with a check that tests one behavior, and git bisect finds the commit where that behavior changed.

04:23 Next, in Exercise: Write a Decisive Check, you’ll write your own check for one problem.

Become a Member to join the conversation.