A bug is in production but was not in last weeks release. How do you find the exact commit that introduced it?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
Use git bisect to binary-search history: mark the current commit bad and an old known-good commit, then Git repeatedly checks out the midpoint. You test each and say good or bad until Git identifies the first bad commit. Automate it with git bisect run <script> for an unattended search.
Detailed explanation
Linearly scanning weeks of commits is slow; bisect turns it into a binary search (log base-2 of N steps). Start with git bisect start, mark git bisect bad (current) and git bisect good <old-sha> (a release tag or commit you trust). Git checks out the midpoint; you run your test, mark it good or bad, and Git halves the range again — repeat until it prints the first bad commit. For speed and repeatability, automate: git bisect run ./test.sh exits 0 for good, non-zero for bad, and Git drives the whole search hands-free. Limit the range with git bisect good <tag> bad <tag>. Skip commits that cannot be tested (build failures) with git bisect skip. Under the hood it is pure binary search on the commit DAG, so merge-heavy histories still resolve correctly.
Example
git bisect start
git bisect bad HEAD
git bisect good v1.4.2
git bisect run ./scripts/check-bug.sh
# Git prints: abc1234 is the first bad commit
git bisect resetInterview tip
Say binary search through history first to frame it, then show the manual good/bad flow and the bisect run automation. Naming the log base-2 complexity signals depth.
Common mistakes
- Linearly reading git log instead of bisecting.
- Not knowing bisect can run unattended with a script.
- Forgetting git bisect reset and leaving the repo in bisect state.