Git Bisect Run to Automate Finding Bad Commits

Git bisect is one of my favourite features of Git. It helps you to perform a binary search on your codebase for a bad commit which introduced a bug.

To do so you first tell Git a bad and good commit to search between for the commit the bug was introduced:

git bisect start <bad_commit> <good_commit>

Git will successively checkout different commits between the range for you to test and tell git if they are good or bad:

git bisect good
git bisect bad

This makes tracing bugs much easier than manually performing this task. What is even better though is git bisect run which automates the determination of whether a commit is good or bad.

If you can write a script or command to detect the bug, which exits with a status code of 0 if the bug is not present and a number in the range 1-127 if it is present, then this can be passed to git bisect run and Git will handle finding the bug for you!

I encountered a bug recently where tests were failing on one machine but not on others. I was able to pass the test command to git bisect run to determine the first commit for which tests failed.

git bisect run npm t

When finished you can checkout the commit you were on before the bisect started with:

git bisect reset
Git
SCROLL TO TOP