bashadvanced

Git Bisect to Find a Bug

Use binary search through commit history with git bisect to find the exact commit that introduced a bug.

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 v1.0.0
# or by hash:
git bisect good abc1234

# Git checks out a middle commit — test it
# If this commit has the bug:
git bisect bad
# If this commit works fine:
git bisect good

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

# End the bisect session
git bisect reset

# Automated bisect with a test script
git bisect start HEAD v1.0.0
git bisect run npm test

# Automated bisect with custom script
git bisect run bash -c 'node -e "require(\"./dist/app\")" 2>/dev/null'

# View bisect log
git bisect log

# Skip a commit that can't be tested
git bisect skip

Use Cases

  • Finding which commit introduced a regression
  • Automated bug hunting across hundreds of commits
  • Debugging intermittent failures in CI

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.