DEV Community

Cover image for Debugging with git bisect: A Smarter Approach to Bug Localization
Vitalii
Vitalii

Posted on

Debugging with git bisect: A Smarter Approach to Bug Localization

Debugging with git bisect: A Smarter Approach to Bug Localization

Recently, I came across an interesting debugging scenario involving a critical production issue. The problem arose after a big release, with a bug that was elusive, browser-specific, and difficult to reproduce locally. Adding to the complexity, the build process for testing each potential fix took around five minutes. The task was to isolate the bug from among 30-35 recent commits - quickly before stakeholders logged in on Monday morning.

The approach taken was to manually perform a binary search using Git, iteratively resetting commits in halves (e.g., git reset --hard HEAD~14) and testing each state. Within half an hour, the culprit—a dependency upgrade—was identified and fixed, ensuring the system was stable before the critical deadline.

This manual approach worked well, but there's a more efficient and systematic way to handle such situations: git bisect.

What is git bisect?

git bisect is a built-in Git tool that automates binary search through your commit history to pinpoint the exact commit that introduced a bug. Instead of manually resetting and testing commits, git bisect systematically narrows down the problematic commit with minimal manual intervention.

How Does git bisect Work?

The process involves three main steps:

  • Mark the Range: Identify a "good" commit where the code worked as expected and a "bad" commit where the bug is present.
  • Automated Testing: Git checks out commits halfway between the good and bad commits, asking you to verify if the issue exists.
  • Narrowing Down: Based on your feedback, Git continues halving the search space until it identifies the first bad commit.

Step 1: Start the Bisect Process

Tell Git where the binary search begins:

git bisect start
git bisect bad HEAD          # Mark the current buggy commit
git bisect good HEAD~35      # Mark the last known working commit
Enter fullscreen mode Exit fullscreen mode

Step 2: Test Commits

Git will automatically check out a commit halfway between the good and bad commits. Test the code to determine whether the bug is present:

  • If the bug exists, mark the commit as bad:
git bisect bad
Enter fullscreen mode Exit fullscreen mode
  • If the bug doesn't exist, mark the commit as good:
git bisect good
Enter fullscreen mode Exit fullscreen mode

Step 3: Repeat Until the Culprit is Found

Git will continue halving the range of commits until it identifies the first bad commit.

Why Use git bisect?

The manual binary search approach can work in smaller ranges, but it comes with limitations:

  • Error-prone: Counting and resetting commits manually can lead to mistakes.
  • Time-consuming: Repeating git reset for each test slows down the debugging process.
  • No Automation: Without automation, testing remains a manual task for each step.

In contrast, git bisect provides:

  • Speed: Cuts down the number of tests needed to isolate the issue.
  • Accuracy: Automates commit checkout, reducing human error.
  • Reproducibility: Easy to rerun or script for consistent results.

Conclusion

Debugging production issues under tight deadlines can be stressful, but tools like git bisect make the process more manageable. It automates the hunt for problematic commits, saving time and effort while ensuring accuracy. If you're not already using git bisect, give it a try - it could be the key to resolving your next tricky bug efficiently.

Have other debugging strategies or tips? Share them below!

Links:

Top comments (0)