Course contentsShow
← Computer Science
Lesson 2208 of 2,87249. Version Control with GitFree lesson

Git Bisect for Bug Hunting

Perform binary search through commit history with git bisect to identify bug-introducing commits.

Git Bisect for Bug Hunting

You'll learn how to use git bisect to automatically pinpoint the exact commit that introduced a bug through binary search.

What Is Git Bisect?

Imagine you discover a bug in your code, but you know it worked fine two weeks ago. Between then and now, there are 128 commits. Manually testing each one would be tedious. git bisect uses binary search to efficiently find the culprit commit—like searching a dictionary by repeatedly dividing the search space in half.

How Binary Search Works on Commit History

Instead of checking all commits linearly, git bisect divides your commit history in half repeatedly. You mark commits as "good" (no bug) or "bad" (bug present), and Git intelligently narrows down the range until it identifies the first commit where the bug appeared.

With 128 commits, binary search needs only about 7 tests instead of 128!

Basic Workflow

Start the bisect session:

git bisect start
git bisect bad          # Current commit has the bug
git bisect good abc123  # This old commit was bug-free

Git checks out a commit halfway between. Test your code, then mark it:

git bisect good  # or 'git bisect bad'

Repeat until Git identifies the problematic commit. Exit with:

git bisect reset

You can even automate testing with a script:

git bisect run ./test_script.sh

Why This Matters

When dealing with hundreds or thousands of commits, bisect transforms an overwhelming debugging task into a systematic, logarithmic hunt. It leverages your commit history as a diagnostic tool, making your Reflog and careful commit practices pay dividends.

Key Takeaway: Git bisect performs binary search through commit history to rapidly identify bug-introducing commits, turning hours of manual testing into minutes of automated investigation.