Git bisect for finding the commit that introduced a bug
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Git bisect runs a binary search across commit history to find which commit introduced a regression. It is the correct tool when a bug exists in the current HEAD, did not exist at some known-good point, and reading 80 commits of diff would take hours.
The binary search workflow
- Mark a known-bad commit:
git bisect bad HEAD; mark a known-good commit:git bisect good v2.4.0; bisect checks out the midpoint commit for you to test - Test the checked-out version — compile, run the failing test, or hit the endpoint — then tell bisect
git bisect goodorgit bisect bad; it halves the remaining search space and checks out the next midpoint - Seven rounds of bisection cover 128 commits; finding the culprit among 80 weekend commits takes about 7 rounds instead of reading all 80 diffs
Automating bisect with a test script
git bisect run mvn test -pl :order-service -Dtest=OrderServiceTestautomates the entire bisection — bisect runs the command at each midpoint and treats exit code 0 as good, non-zero as bad- The test must be deterministic; a flaky test produces a false bad result that misdirects bisect to an innocent commit
- Write the reproduction test before starting bisect — having a precise, isolated test is what makes automation possible and the result reliable
After finding the culprit commit
git bisect resetreturns the repository to the original HEAD after the search completes- Examine the identified commit with
git show— the diff will be small (bisect found a specific commit, not a range) and the bug is usually obvious in context git log --onelinelists all commits in the bad range if you want to understand what changed in that window before bisect narrows it down..
Git bisect for finding the commit that introduced a bug
Direct Answer
Git bisect performs a binary search through commit history to find the first commit where a condition changed — typically where a bug was introduced. You mark one commit as good (bug absent) and one as bad (bug present), and Git checks out the midpoint. You test it, mark it good or bad, and repeat until Git identifies the exact offending commit. The process takes O(log n) steps — finding the culprit in 1000 commits takes about 10 steps.
Check your understanding
Answer these without looking back at the article.
- What happens if a commit in the bisect range is untestable — e.g., it doesn't compile?
- How would you use `git bisect log` and `git bisect replay` to document a debugging session?
- Can git bisect be used to find when a performance regression was introduced? How?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.