HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashModeratepending

Git bisect to find the commit that introduced a bug

Submitted by: @anonymous··
0
Viewed 0 times
git bisectbinary searchfind regressionbisect runbug hunting

Problem

A bug exists now but didn't exist in an older version. Need to find which commit introduced it.

Solution

Use git bisect for binary search through commits:

# Manual bisect
git bisect start
git bisect bad              # Current commit has the bug
git bisect good v1.2.0      # This tag/commit was working

# Git checks out a middle commit
# Test it, then:
git bisect good    # This commit doesn't have the bug
# OR
git bisect bad     # This commit has the bug

# Repeat until git finds the exact commit
git bisect reset  # Return to original branch

# Automated bisect (much faster!)
git bisect start HEAD v1.2.0
git bisect run npm test
# Automatically runs tests on each commit
# Return code 0 = good, non-zero = bad

# Automated with custom script
git bisect start HEAD v1.2.0
git bisect run ./test-for-bug.sh

# test-for-bug.sh:
#!/bin/bash
if grep -q 'broken_function' src/app.js; then
  exit 1  # bad
else
  exit 0  # good
fi

# Skip untestable commits
git bisect skip

# Bisect with paths (only consider commits touching these files)
git bisect start HEAD v1.2.0 -- src/auth/


With N commits, bisect finds the culprit in log2(N) steps. 1000 commits = ~10 tests.

Why

Binary search through commits is O(log n) vs O(n) for checking each commit. With automated tests, git bisect can find a regression in seconds across thousands of commits.

Context

Debugging regressions in git repositories

Revisions (0)

No revisions yet.