Sometimes in your day-to-day development routine, you might face an issue introduced some times ago, but you don’t know when exactly. You probably have a git history of your project, so you can check different versions to understand where the issue was introduced.
But how to do that? For sure, you can checkout every single commit and run the code to find the problematic commit. But if you have a lot of commits, it could be boring. This is where git bisect shines. Git allows you to utilize the power of binary search algorithm to find the problem quickly.
To use git bisect, you need to find some commit in the past where the issue doesn’t exist and let git know commits that are known as “good“ and “bad“:
$ git bisect start $ git bisect bad $ git bisect good 39ab046
git bisect bad without additional parameters means that the current commit is bad. You can also specify some commit explicitly (you can also use git tag name instead of commit sha):
$ git bisect start $ git bisect bad 869d470 $ git bisect good 39ab046
The shorter form of command to start binary search is to define bad, and good commit as parameters of
git bisect start command (first parameter is a bad commit, second — a good commit):
$ git bisect start 869d470 39ab046
Once you start binary search with by of the previous ways, you need to check your code and determine whether it still has the problem (run the code, run tests, or anything else). If the current state of your code is okay, then you run
git bisect good; otherwise -
git bisect bad. Next, you will need to repeat it until you find the commit that introduced the initial issue.
git bisect run
In case you have some automated way to check whether the commit is good or bad, you can delegate this run to
git bisect. It could be tests run, some shell script run, or anything else. To do that, start bisect with the
start command and then use
npm bisect run. For instance, if we have
npm run test script to run the tests, then we can run bisect the following way:
$ git bisect start 869d470 39ab046 $ git bisect run npm run test
This works as follows: