bashintermediate

Git Bisect — Find the Commit That Broke It

Use binary search across commits to pinpoint exactly which commit introduced a bug or regression.

bash
# Start bisect session
git bisect start

# Mark current commit as bad (has the bug)
git bisect bad

# Mark a known good commit (before the bug)
git bisect good v2.1.0

# Git checks out a middle commit. Test it, then:
git bisect good   # if this commit works
git bisect bad    # if this commit is broken

# Repeat until Git finds the exact bad commit:
# "abc1234 is the first bad commit"

# End bisect and return to original branch:
git bisect reset

# --- Automated bisect with a test script ---
git bisect start HEAD v2.1.0
git bisect run npm test
# Git runs 'npm test' on each commit automatically,
# marks good/bad based on exit code (0 = good, 1+ = bad).

# Automated bisect with custom script:
git bisect run bash -c 'grep -q "expectedFunction" src/app.ts'

Sponsored

GitHub Copilot

Use Cases

  • Finding which commit introduced a regression
  • Automating bug-hunting with test scripts
  • Narrowing down performance degradation causes

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.