# Bisecting Bugs

> Use binary search to rapidly hunt down the exact commit that introduced a bug.

Subject: Version Control | Lesson 28 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-bisect
Previous: [The Git Stash](https://codewithanimation.in/git/git-stash.md) | Next: [Git Hooks](https://codewithanimation.in/git/git-hooks.md)

## The Bug Hunter

`git bisect` is a command that uses a binary search algorithm to efficiently find the exact commit in a project's history that introduced a bug.

## Why it matters

Web developers frequently work in large codebases. When a subtle UI regression is discovered but it's unclear when it happened, `git bisect` drastically reduces the time needed to hunt down the exact commit.

By isolating the specific code change that caused the issue, you can understand the context and apply a fix much faster instead of blindly guessing.

## How it works

You start by identifying a 'bad' commit (usually your current broken state) and a 'good' commit (an older state where the bug didn't exist).

Git checks out a commit exactly in the middle. You test it and mark it as `good` or `bad`. Git halves the remaining commits and repeats this process until the single offending commit is isolated.

## Common mistake: The False Start

The #1 common mistake is not verifying the initial 'good' commit actually works.

Developers often blindly assume an older commit is 'good', only to discover halfway through the bisect that the older version wouldn't even build, rendering the entire search invalid and forcing them to start over.

## Tip: Automate the Search

Pro Tip: Automate the process using `git bisect run <script>`.

If you have a unit test or script that checks for the bug (returning 0 for good, 1 for bad), Git will run the script on each checkout and automatically mark it, saving you from doing any manual testing.

## Try it: Narrow down a regression

Test the selected commit and mark its result to locate the first bad commit.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-bisect

## Check yourself

### Question 1

What algorithm does `git bisect` use under the hood to find a bug efficiently?

- A. Linear search.
- B. Binary search.
- C. Depth-first search.
- D. Hash matching.

Correct answer: B

Git halves the commit history between a known good and bad commit to efficiently isolate the offending commit in logarithmic time.

- Why not A: Checking every single commit one by one would be far too slow.
- Why not C: DFS is for graphs/trees, not searching a sorted timeline.
- Why not D: Hashes uniquely identify commits, but don't locate bugs.

### Question 2

You are in the middle of a `git bisect` session, and the current commit Git checked out has a syntax error that prevents the app from running. What should you do?

- A. Run `git bisect bad`.
- B. Abort the bisect process.
- C. Edit the code to fix the syntax error and commit it.
- D. Run `git bisect skip`.

Correct answer: D

This tells Git to ignore the untestable commit and pick a different nearby commit to test instead.

- Why not A: Marking it bad implies this commit has the specific bug you are looking for.
- Why not B: You don't need to start over.
- Why not C: Do not make new commits during a bisect session.

### Question 3

How can you fully automate a `git bisect` process so you don't have to manually test each step?

- A. Provide a test script using `git bisect run`.
- B. Use `git bisect auto`.
- C. Use a GitHub Action.
- D. Write a loop in Bash to iterate over `git log`.

Correct answer: A

`git bisect run <script>` will execute your script on each checkout, using its exit code to automatically mark commits.

- Why not B: This is not a valid Git command.
- Why not C: CI/CD can run tests, but `bisect run` automates the local search.
- Why not D: This defeats the purpose of the built-in automation.

## Remember this

- Bisect uses a binary search to find bad commits fast.
- Always start by defining a known good and known bad commit.
- Use `git bisect skip` if a commit is untestable.
- Use `git bisect run` to automate the search with tests.
