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
git bisect startgit bisect bad(current commit has the bug)git bisect good v1.2.0(this version was fine)- Git checks out a midpoint commit
- Test the code, then run
git bisect goodorgit bisect bad - Repeat until Git identifies the offending commit
git bisect resetto 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
- Bisect works best with a clean, linear history and automated tests
- For resolving conflicts found during bisect, see how to resolve Git merge conflicts