Git & GitHub
/Advanced
git bisect
Definition
A command that uses a binary search algorithm to find which commit in your project's history introduced a bug.
Explain Like I'm New
The ultimate bug hunting tool. You tell Git: 'The code is broken right now. But I know for a fact it was working 50 commits ago.' Git will automatically jump to the middle commit (commit 25) and ask: 'Is it broken here?'. You say Yes or No. It jumps again, splitting the list in half. Within 5 questions, it finds the exact commit that caused the bug out of hundreds.
Real World Example
Finding a memory leak. You can't figure out when the leak was introduced. You start `bisect`, run your test, and narrow it down to the exact developer and the exact line of code that caused the leak.
Common Use Cases
- •Hunting down regressions
- •Debugging massive codebases
Terminal Output
bash / terminal
# 1. Start the wizard
$ git bisect start
# 2. Tell Git that right now, the code is broken
$ git bisect bad
# 3. Tell Git a hash from 3 weeks ago where the code was definitely working
$ git bisect good a1b2c3d
# Git jumps to a commit halfway between them.
# You test the app in your browser...
# 4. If it's broken, type: git bisect bad
# 4. If it works, type: git bisect good
# Repeat until Git says: "9f8e7d6 is the first bad commit!"
# 5. End the wizard and go back to normal
$ git bisect reset
Interview Questions
basic
- What two points in time must you give Git to start a bisect?
intermediate
- Can you automate `git bisect` so you don't have to manually answer Yes/No?