Git without fear · Chapter 25 of 28

Find the bug with bisect

By the end of this chapter you can find the commit that broke something in a few steps, however long the history, and undo it without rewriting anything. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Detective work
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Meera is back with a printed bill: masala chai, two cutting chai and a bun maska. That is 140, and the bill says 100. Tapri has been undercharging all weekend. At the v1.0 release the bill was right; since then there are 21 commits, most of them Bunty's ("I only changed little things!").

You could check out the commits one by one and test each, but that is up to 21 checkouts, and reading the diffs instead hides the bug among menu changes and opening hours. You want Git to pick the next commit to test, and to need as few as possible.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Tag the first bad commit bill-bug

    The bill was right at v1.0 and is wrong on main. Find the commit that broke it.

    1. Run ./check-bill.sh. It says bill ok or bill WRONG.

    2. Use git bisect to find the first bad commit (task 2).

    3. When Git names the first bad commit, tag it bill-bug. Do this before you end the bisect.

    To pass: a tag called bill-bug points at the commit that broke the bill. It must be Bunty's original commit, not a copy of it or a commit near it.

  2. Task 2

    Find it with git bisect, then end the bisect

    Let git bisect find the bad commit for you, then go back to main.

    1. Run git bisect start.

    2. Tell Git that the commit you are on is bad, and that v1.0 was good.

    3. Test each commit Git picks with ./check-bill.sh, and answer good or bad. Or let Git run the script for you.

    4. After you tag the bad commit, end the bisect. Git takes you back to main.

    To pass: you ran git bisect start in the chapter folder and it worked, no bisect is still running, and you are on the branch main.

  3. Task 3

    Fix the bill on main with a new commit, keeping the history

    main is shared history, so fix the bill with a new commit. Do not rewrite main.

    1. On main, make a new commit that undoes the commit you tagged bill-bug. git revert does this.

    2. Run ./check-bill.sh. It should say bill ok.

    To pass: check-bill.sh says bill ok on the files committed on main (not only on your disk). The bill-bug commit and "Fix the README title" are still in main's history.

  4. Task 4 · interview question

    Interview: say what kind of search git bisect does

    Interview question: what kind of search does git bisect do?

    1. Think about what happens to the commits left to test after each good or bad answer.

    2. Write the name of this search into answer.txt.

    To pass: answer.txt names the search, or says what happens to the commits left after each answer.

What you will learn

  • git bisect: a binary search through history, from a good commit to a bad one.
  • Good or bad: you test the commit Git picks and answer, and the range halves each time.
  • git bisect run: let a test script give the answers.
  • Ending it: tag the culprit, go back to main, and fix it with a new commit.

More in Detective work

Build "Find the bug with bisect" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.