Git Bisect: Finding the Commit That Broke Everything
The setup
A feature that had been working started failing. No one noticed when — it was a subtle off-by-one that only affected a specific edge case.
The bisect
# Start the bisect — mark current as bad
git bisect start
git bisect bad
# Mark a known-good commit
git bisect good HEAD~60
# Bisect runs 6-7 steps, each time:
# "Is this commit good or bad?"
# Answer: git bisect good or git bisect bad
# Automatically runs a script to test
git bisect run npm test -- --filter=cache-layer
Found the commit in 7 steps. It was a one-line change that accidentally swapped two arguments.
The automation trick
# Create a test script that exits 0 for good, non-0 for bad
echo 'npm test -- --filter=cache-layer' > /tmp/test.sh
chmod +x /tmp/test.sh
git bisect run /tmp/test.sh
What I learned
Bisect with run is a superpower. It automates the entire search. The key is having a reliable test script that returns a clean exit code. If your tests are flaky, bisect is useless.