DEV Community

JLarky
JLarky

Posted on

Using Git Bisect to Find the Commit that Broke ESLint

In the world of software development, bugs and errors are inevitable. Even with the most comprehensive testing suite, some bugs might manage to slip into your codebase. So, when something goes wrong, how do you find the culprit commit?

Luckily, Git provides a powerful tool for this very purpose, named git bisect. It uses a binary search algorithm to quickly and efficiently find the exact commit that introduced a bug.

In this blog post, we will guide you through the process of using git bisect, specifically with the git bisect run command, to debug a failing yarn lint command.

Understanding Git Bisect

Git bisect is a binary search tool that helps you discover which specific commit introduced an error. This works by moving between commits, effectively halving the search range each time, until it narrows down to the faulty commit.

You mark the known good and bad commits, and git bisect will do the rest. It's an extremely useful tool for large repositories where manually checking each commit would be tedious or nearly impossible.

Preparing for Bisect

Before we start, ensure your repository is in a clean state; any changes should be committed or stashed. This is important because git bisect will be switching between different commits during its search.

Starting the Bisect Process

The first step is to start the bisect process with the following command:

git bisect start
Enter fullscreen mode Exit fullscreen mode

Now, we need to inform Git about the state of our current commit. Since we know that our current commit is causing the yarn lint command to fail, we can mark it as "bad":

git bisect bad
Enter fullscreen mode Exit fullscreen mode

Next, we need to specify a commit where we know that everything was working as expected - a "good" commit. Let's assume that the origin/main branch does not have the bug. We can mark it as good with:

git bisect good origin/main
Enter fullscreen mode Exit fullscreen mode

Once you do this, Git will automatically checkout a commit in the middle of the range between the good and bad commits.

Automating the Bisect Process

Here's where the magic happens. Instead of manually checking if each commit is good or bad, we can automate the process with git bisect run.

git bisect run yarn lint
Enter fullscreen mode Exit fullscreen mode

In this case, Git will keep bisecting without your intervention until it finds the first bad commit. It will run the yarn lint command on each commit, and if the command succeeds (returns 0), the commit is considered good. If it fails (returns anything other than 0), the commit is marked as bad.

Once the faulty commit has been identified, Git will present a message like This is the first bad commit along with the commit details.

Wrapping Up the Bisect Process

After Git has found the first bad commit, we need to end the bisect process. We do this with:

git bisect reset
Enter fullscreen mode Exit fullscreen mode

This will take you back to the commit you were on when you started the bisecting.

Conclusion

Using git bisect run can significantly simplify the process of identifying a problematic commit, especially in larger codebases. With the help of an automated command like yarn lint, it makes it easier than ever to isolate issues and keep your project running smoothly. Understanding and using such powerful tools is what separates good developers from great ones, and is a step forward in writing more reliable, robust software.

Top comments (0)