>_ IzzyLab

Hands-on task

This task is step 18 of the course “Git: rewriting and rescuing history”

Find the breaking commit with bisect run

hard~20 min

In /root/repo the main branch is a long linear history with identical-looking messages (step N). The script sum.sh N must print the sum of the numbers from 1 to N (bash sum.sh 10 → 55). In the very first commit it computes correctly, in HEAD it no longer does. Find the first commit where the result became wrong and write its full hash into /root/bad.txt.

The catch: several commits in the middle of the history don't run at all (a syntax error) — you can't tell whether they're good or bad. Stepping through the commits by hand is slow — let git split the history in half itself, running your check at each step.

What you'll practise:
  • git bisect
  • binary search
  • rev-list
  • check automation
  • regression hunting

Topic breakdown

When a regression sneaks in somewhere within a long linear history, stepping through the commits one by one is O(n); binary search does it in O(log n). git bisect takes a known-bad and a known-good revision (the very first commit is conveniently obtained via git rev-list --max-parents=0) and splits the range in half, asking for a verdict at each step. The check can be automated with a script that exits with code 0 on the "good" state and non-zero on the "bad" one; when it finishes, HEAD sits on the first bad commit, and the working tree is restored with reset.

What's checked

  1. full hash written
  2. this is the first bad commit

What it looks like