git bisect — find the commit that introduced a bug
Quick Answer
# Start bisect, mark current state as bad
git bisect start
git bisect bad # current commit has the bug
# Mark a known-good commit (a tag, hash, or relative ref)
git bisect good v1.2.0
# git checks out the midpoint — test it, then mark it:
git bisect good # or: git bisect bad
# Repeat until git prints: "abc1234 is the first bad commit"
# When done, restore HEAD
git bisect reset
When to use this
A bug appeared somewhere between a known-good release and the current HEAD. Bisect finds the exact commit in O(log n) steps.
Other causes & fixes
Automate the test with a script
If you have a test command that exits 0 for good and non-zero for bad, bisect can run automatically:
git bisect start
git bisect bad HEAD
git bisect good v1.2.0
git bisect run npm test -- --grep "failing test name"
Skip a commit you cannot test
git bisect skip
Related