Bisect in Git

02 Mins
🔁
Course Under Active Revision Re-visiting

This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.

Git bisect is a powerful tool for finding bugs. It performs a binary search on your commit history to find the exact commit where a bug was introduced.

Instead of testing every commit linearly in $O(N)$ time, git bisect tests commits logarithmically in $O(\log N)$ steps.

1. How Git Bisect Works

Git bisect requires two reference boundaries:

  • A bad commit (where the bug is currently present)
  • A good commit (an older commit where the bug was not present)

Git repeatedly checks out the midpoint between the good and bad commits in a binary search pattern until the exact offending commit is isolated.

2. Interactive Bisect Debugging Explorer

Step through the interactive visualizer below to see how Git bisects the commit range, tests the midpoint, and zeroes in on the broken revision:

Git Bisect: Binary Search Debugging

Step 1 of 4: Mark Endpoints: Known Good and Bad
1 / 4
🎯 Active Search Window: [ #0 ... #5 ] Binary Search Progress
Step #0 c1 ✓ GOOD
Step #1 c2 UNTESTED
Step #2 c3 🔍 TESTING
Step #3 c4 UNTESTED
Step #4 c5 UNTESTED
Step #5 c6 ✗ BAD
Testing commit c3. If tests pass, search right half; if tests fail, search left half. O(log N)
$ git bisect start && git bisect bad HEAD && git bisect good c1

Commit #0 works; commit #5 is broken. Git initiates binary search over the range [0..5].


3. Example Walkthrough: Finding a Regression

Suppose we are writing a Java addition utility:

import java.util.*;

public class sum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number: ");
        int a = sc.nextInt();
        System.out.println("Enter the second number: ");
        int b = sc.nextInt();
        int sum = add(a, b);
        System.out.println("The sum of the two numbers is: " + sum);
    }

    public static int add(int a, int b) {
        a + b; // Missing return statement
    }
}

Step 1: Got error at one point

We have multiple commits, and one of them broke our build:

Untitled

Step 2: Start Git Bisect

Specify your initial boundaries:

git bisect start
git bisect bad 8c76244   # Broken commit
git bisect good 11d3ee4  # Known working commit

Untitled

Step 3: Test the Midpoint Commit

Git automatically checks out the midpoint commit. Run your compiler or test suite:

# If tests fail:
git bisect bad

# If tests pass:
git bisect good

Step 4: Repeat Until Culprit is Found

Git halves the search window on each iteration until it pinpoints the first bad commit:

Untitled

Git highlights the exact commit that introduced the bug, saving hours of manual code inspection.

Step 5: Exit Git Bisect

Once the bug is identified, exit bisect mode to return your working tree to your original branch:

git bisect reset

4. Automating with git bisect run

If you have an automated test command that returns exit code 0 on success and non-zero on failure:

git bisect start HEAD v1.0.0
git bisect run npm test

Git will automatically run the tests on each midpoint until it isolates the broken commit without requiring manual input.