Git Bisect: Find the Commit That Introduced a Bug

Use git bisect to perform binary search through commit history and find exactly when a bug was introduced. Learn manual and automated bisecting techniques.

Advanced

Detailed Explanation

Finding Bugs with Git Bisect

git bisect uses binary search to efficiently find the commit that introduced a bug. Instead of checking every commit, it halves the search space each step, making it logarithmically fast.

Manual Bisect

# Step 1: Start bisecting
git bisect start

# Step 2: Mark the current commit as bad (has the bug)
git bisect bad

# Step 3: Mark a known good commit
git bisect good v2.0.0

# Git checks out a middle commit. Test it, then:
git bisect good   # if this commit does NOT have the bug
# or
git bisect bad    # if this commit HAS the bug

# Repeat until git identifies the culprit
# Git will output: "<hash> is the first bad commit"

# Step 4: Reset when done
git bisect reset

Automated Bisect

Use a test script to automate the process:

# The script should exit 0 for good, non-zero for bad
git bisect start HEAD v2.0.0
git bisect run npm test

# With a custom script
git bisect run ./test-bug.sh

# The script might look like:
# #!/bin/bash
# npm run build && npm run test:specific -- --grep "login"

Bisect with a Skip

If a commit cannot be tested (broken build, etc.):

git bisect skip

Viewing Bisect Progress

# Show the bisect log
git bisect log

# Visualize the remaining commits
git bisect visualize

Replaying a Bisect

# Save the bisect log
git bisect log > bisect.log

# Replay it later
git bisect replay bisect.log

Efficiency

For N commits between good and bad, bisect needs at most log2(N) steps:

  • 100 commits: ~7 steps
  • 1,000 commits: ~10 steps
  • 10,000 commits: ~14 steps

This makes bisect far more efficient than linear searching through commits.

Use Case

Git bisect is invaluable when a bug appears but you do not know which commit introduced it. This is common in large projects with many contributors where a regression is discovered days or weeks after it was introduced. Automated bisect with test scripts is especially powerful -- you can identify the exact commit that broke a test suite across hundreds of commits in minutes. It is a skill every developer should have in their debugging toolkit.

Try It — Git Command Reference & Cheat Sheet

Open full tool