How to use git bisect to find bugs
· Category: Git
Short answer
git bisect performs a binary search between a good and bad commit to find the exact commit that introduced a bug.
Steps
- Start bisect:
git bisect start
- Mark the current commit as bad:
git bisect bad
- Mark a known good commit:
git bisect good v1.0.0
- Git checks out a middle commit; test and mark good or bad.
- Repeat until the culprit is found, then reset:
git bisect reset
Tips
- Automate testing with
git bisect run <script>. - Choose the widest possible range for faster narrowing.
- Document the bad commit in your issue tracker.
Common issues
- Flaky tests can mislead bisect; ensure reliable reproduction.
- Skipping commits with
git bisect skiphelps when a commit is unbuildable.