What is git bisect and how does it find bugs?
Learn how git bisect uses binary search across commit history to pinpoint the exact commit that introduced a bug, including the automated bisect run workflow.
Expected Interview Answer
git bisect is a binary-search tool that finds the exact commit which introduced a bug by repeatedly checking out a commit halfway between a known good commit and a known bad commit, letting you test and mark each one, until it narrows the culprit down to a single commit.
You start with git bisect start, then mark a commit known to have the bug as bad and an older commit known to work as good. Git checks out the midpoint commit; you test it and report git bisect good or git bisect bad, and Git halves the remaining range again. This continues logarithmically, so even thousands of commits collapse to the guilty one in roughly log2(n) steps. You can also automate the whole process with git bisect run <script> if you have a test that exits non-zero on failure, letting Git test every step itself without manual intervention.
- Finds the exact regression-causing commit efficiently
- Scales logarithmically even across thousands of commits
- Can be fully automated with a pass/fail test script
- Turns vague 'it broke sometime' into a precise commit
- Much faster than manually checking out commits one by one
AI Mentor Explanation
git bisect is like a physio pinpointing which training session caused a nagging injury by checking a session from the middle of the timeline, seeing if pain was present, then repeating on whichever half still contains the answer. Instead of reviewing every session, each check eliminates half the candidates, so months of logs collapse to the culprit session in a handful of checks.
Step-by-Step Explanation
Step 1
Start the bisect session
Run git bisect start to begin the binary search over your commit history.
Step 2
Mark the boundaries
Mark the current buggy commit with git bisect bad and an older working commit with git bisect good <hash>.
Step 3
Test the midpoint
Git checks out the commit halfway between good and bad; you build and test it manually.
Step 4
Report the result
Run git bisect good or git bisect bad based on your test, and Git halves the remaining range again.
Step 5
Automate or finish
Repeat until Git reports the first bad commit, or use git bisect run <script> to automate every step with a pass/fail test.
What Interviewer Expects
- Explains bisect as a binary search over commit history
- Knows the good/bad marking workflow
- Understands the logarithmic efficiency (log2 of commit count)
- Mentions git bisect run for automated testing
- Knows to run git bisect reset to return to the original branch afterward
Common Mistakes
- Testing commits linearly instead of trusting the binary search
- Forgetting to run git bisect reset after finishing
- Marking a commit good/bad incorrectly, corrupting the search
- Not knowing git bisect run exists for automatable tests
Best Answer (HR Friendly)
“git bisect helps you find exactly which change introduced a bug by testing points in the middle of your project's history and narrowing down the search, similar to finding a word in a dictionary by repeatedly splitting it in half. It turns a vague 'somewhere in the last few months' into a single exact commit very quickly.”
Code Example
git bisect start
git bisect bad # current commit has the bug
git bisect good v1.2.0 # this old tag was known to work
# Git checks out the midpoint; test it, then:
git bisect good # or: git bisect bad
# ... repeat until Git reports the culprit ...
git bisect reset
# Fully automated with a test script
git bisect start HEAD v1.2.0
git bisect run npm testFollow-up Questions
- How does git bisect run automate the search with a test script?
- What does git bisect reset do and why is it important?
- How many steps would bisect need for 1000 commits?
- Can git bisect skip a commit that cannot be tested? How?
- How would you combine git bisect with CI to find regressions automatically?
MCQ Practice
1. What algorithm does git bisect use to find a bad commit?
git bisect repeatedly checks the midpoint commit between good and bad boundaries, halving the search space each time.
2. Roughly how many test steps does bisect need for 1000 commits?
Binary search needs about log2(n) steps, so around 10 tests can pinpoint the culprit among 1000 commits.
3. What does git bisect run automate?
git bisect run <script> lets Git test each candidate commit itself using an exit-code-based script, removing manual steps.
Flash Cards
What does git bisect do? — Binary-searches commit history between a known good and bad commit to find the exact commit that introduced a bug.
How do you mark commits during a bisect session? — git bisect good <hash> and git bisect bad mark the boundaries; Git checks out the midpoint each time.
How can you automate git bisect? — git bisect run <script> runs a pass/fail test at each step automatically instead of manual testing.
Why is bisect efficient for large histories? — It needs only about log2(n) tests, so even thousands of commits resolve in a handful of steps.