What is Git bisect and how to use it for debugging

· Category: Git

Short answer

git bisect performs a binary search through your commit history to find which commit introduced a bug. Start it with git bisect start, mark the current commit as bad, mark a known-good commit, and Git checks out the midpoint for you to test. For more Git debugging tools, see how to revert a commit in Git.

Steps

  1. git bisect start
  2. git bisect bad (current commit has the bug)
  3. git bisect good v1.2.0 (this version was fine)
  4. Git checks out a midpoint commit
  5. Test the code, then run git bisect good or git bisect bad
  6. Repeat until Git identifies the offending commit
  7. git bisect reset to return to your original branch

Automating bisect

git bisect run npm test

This automatically runs your test suite at each step, marking commits as good or bad based on the exit code.

Tips