The shipping total for a standard parcel jumps from 500 to 5000 cents. The latest release is wrong, but its most recent commit only edits the README. git bisect lets you ask which change first made a repeatable check fail. This lab creates a six-commit Git history and gives Git a Python probe that checks the same shipping rule at every step. You can see the offending diff and then return to your original branch.

Set up a disposable repository

You need Git 2.28 or later, Python 3.10 or later, and a terminal with both commands available. Use python3 wherever the examples say python if that is how Python is installed. The lab needs no packages, accounts, remotes or network access. Download the complete Git bisect lab (ZIP), extract it and open the git-bisect-shipping folder in your terminal. You can also save the two source listings below into a fresh folder.

Run the fixture builder once. It creates a sibling directory called shipping-lab alongside the two scripts. Inside the new repository it creates a local demo identity, commits the sample shipping program and documentation, and tags the first and last commits. If the directory already exists, setup stops without overwriting it. Extract a fresh copy to build another history.

git --version
python --version
python make_fixture.py
cd shipping-lab
git log --oneline --decorate

Check both ends of the search

Run the same probe on known-good and on main, which starts at known-bad. The first run should print 500 cents and succeed. The second should print 5000 cents and exit with status 1. That second failure is the one you expect. If either endpoint disagrees, fix the test or choose different boundaries before starting. A commit you remember as working is only useful once it passes this exact check.

git switch --detach known-good
python ../probe.py
git switch main
python ../probe.py

The probe stays one directory above the repository, so checking out an old commit cannot replace the test itself. It launches that revision's shipping.py using the same Python interpreter, checks that the program printed one integer, and compares it with the agreed price. It reports status 0 for the expected total, 1 for a different total, or 125 if it cannot get a meaningful result.

Find and inspect the first bad commit

Start with the bad revision first and the good revision second. Git checks out a candidate within the range. The run command repeats your probe on the candidates Git chooses, shrinking the interval until it can name the first bad commit. In this straight history, testing a good candidate rules out everything at or before it. Testing a bad one moves the upper boundary back. The search assumes the same failure persists after it appears within the range.

git bisect start known-bad known-good
git bisect run python ../probe.py
git bisect log
git show --oneline refs/bisect/bad -- shipping.py
git bisect reset
git status --short --branch

Git reports the hash and subject Change flat shipping rate. The diff shows print(500) replaced with print(5000). Inspect the result before resetting, since reset removes the temporary bisect refs. After reset the status should be ## main. The original branch still points to its original commit. Bisect found the change; it did not repair the rate.

Here are the key lines from the run. Your commit hash and intermediate messages can differ.

Shipping: 500 cents (expected 500).
Shipping: 5000 cents (expected 500).
<hash> is the first bad commit
Change flat shipping rate
-print(500)
+print(5000)
## main

Handle a revision you cannot test

A missing file, a nonzero program exit, malformed output or a timeout yields status 125 here. Git skips that revision because those outcomes do not establish whether the shipping price was wrong. If a skipped revision sits next to the change, Git may return several possible commits. Use the log to see the gap, fix the test environment if you can, and rerun from the same verified endpoints. If Python itself is missing, restore it to your terminal's PATH before running the probe.

In a real project, reproduce one symptom with a fixed input and run the check a few times before bisecting. If it fails intermittently, you can move the search boundary in the wrong direction. If the bug was fixed and later reintroduced, narrow your endpoints to one occurrence. After Git points to a change, inspect its parent and add a regression test for the behavior you want to keep.

Complete source

make_fixture.py

from pathlib import Path
import os
import shutil
import subprocess
import sys


def main():
    git = shutil.which('git')
    if git is None:
        print('Git must be on PATH.', file=sys.stderr)
        return 2

    repo = Path(__file__).resolve().parent / 'shipping-lab'
    try:
        repo.mkdir()
    except FileExistsError:
        print('shipping-lab already exists; extract a fresh copy to start over.',
              file=sys.stderr)
        return 2

    # Keep this disposable repo independent of the reader's Git configuration.
    env = {key: value for key, value in os.environ.items()
           if not key.upper().startswith('GIT_')}
    env.update(GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull)

    def run(*args):
        subprocess.run([git, *args], cwd=repo, env=env, check=True)

    def commit(filename, contents, subject):
        (repo / filename).write_text(contents + '\n', encoding='utf-8')
        run('add', '--', filename)
        run('commit', '--quiet', '-m', subject)

    try:
        run('init', '--quiet', '--initial-branch=main', '--template=')
        run('config', 'user.name', 'Toolzify Demo')
        run('config', 'user.email', 'demo@example.invalid')
        commit('shipping.py', 'print(500)', 'Add flat shipping rate')
        run('tag', 'known-good')
        commit('README.md', 'Shipping lab', 'Document the lab')
        commit('notes.txt', 'Example: standard parcel', 'Add parcel notes')
        commit('shipping.py', 'print(5000)', 'Change flat shipping rate')
        commit('notes.txt', 'Example: small standard parcel', 'Refine parcel notes')
        commit('README.md', 'Shipping lab for a standard parcel', 'Clarify lab title')
        run('tag', 'known-bad')
    except (OSError, subprocess.CalledProcessError) as exc:
        print(f'Could not build the fixture: {exc}', file=sys.stderr)
        return 2
    print('Created shipping-lab with known-good and known-bad tags.')
    return 0


if __name__ == '__main__':
    sys.exit(main())

probe.py

from pathlib import Path
import re
import subprocess
import sys


def main():
    program = Path('shipping.py')
    if not program.is_file():
        print('Cannot test: shipping.py is missing.', file=sys.stderr)
        return 125
    try:
        result = subprocess.run(
            [sys.executable, str(program)], capture_output=True,
            text=True, encoding='utf-8', timeout=5, check=False,
        )
    except (OSError, UnicodeError, subprocess.TimeoutExpired) as exc:
        print(f'Cannot test: {exc}', file=sys.stderr)
        return 125

    output = result.stdout.strip()
    if result.returncode != 0 or not re.fullmatch(r'[0-9]+', output):
        print('Cannot test: expected a successful integer output.', file=sys.stderr)
        return 125
    print(f'Shipping: {output} cents (expected 500).')
    return 0 if (output.lstrip('0') or '0') == '500' else 1


if __name__ == '__main__':
    sys.exit(main())

Toolzify Editorial ran the fixture and checked the result again from an extracted ZIP. The checks covered the good and bad prices, untestable revisions, the reported diff, and the return to a clean main branch. For the full set of commands and exit statuses, see the Git bisect manual and Pro Git's debugging chapter.