DevOpsGitHub

Git Bisect: Finding Bugs with Binary Search

TT
TopicTrick Team
Git Bisect: Finding Bugs with Binary Search

Git Bisect: Finding Bugs with Binary Search


1. How Bisect Works: The Logic of Two Halves

Git Bisect asks you to define two points in time:

  • A Good Commit: A point in the past where the app worked.
  • A Bad Commit: The current point where the app is broken.

Git will then jump to the Middle Commit. You test the app and tell Git: "Is it good or bad?" Git then discards half of the commits and jumps to the next middle point.


2. The Manual Bisect Workflow

  1. Start: git bisect start
  2. Define Bad: git bisect bad (Current)
  3. Define Good: git bisect good [commit-id-from-monday]
  4. The Loop:
    • Git checkouts a commit.
    • You try the app.
    • Type git bisect good or git bisect bad.
  5. The Result: Git prints the name of the "First Bad Commit" and who wrote it.
  6. Clean up: git bisect reset to go back to normal.

3. The Professional Speed: git bisect run

If you have a Unit Test that can detect the bug, you can automate the entire search!

bash

Git will automatically checkout every commit, run the test, see if it fails (exit code 1) or passes (exit code 0), and finish the job in 2 seconds. This is how world-class site-reliability engineers (SREs) find "Needles in a haystack" within massive, million-line repositories.


4. Why use Bisect over Standard Debugging?

Standard debugging (like setting breakpoints) is good for finding How a bug works. Git Bisect is for finding When it was introduced. By seeing the diff of the "Bad Commit," the cause of the bug often becomes 100% obvious, saving you hours of searching through unrelated code.


Frequently Asked Questions

What if I find a commit that is "Untestable" (e.g., won't build)? You can type git bisect skip. Git will jump to an adjacent commit and continue the binary search around the broken point.

How many steps does it take? For 1,000 commits, it takes only 10 steps. For 1,000,000 commits, it takes only 20 steps. Binary search is the most efficient lookup algorithm in computer science.


Key Takeaway

Git Bisect is the "Time Machine" debugger. By focusing on the Point of Introduction, you eliminate the guesswork of debugging and gain the absolute certainty required to fix production regressions on day one.

Read next: Git Tagging & Releases: Semantic Versioning Strategy →


Part of the GitHub Mastery Course — masters of search.