Bisecting Bugs
Use binary search to rapidly hunt down the exact commit that introduced a bug.
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.
Narrow down a regression
Test the selected commit and mark its result to locate the first bad commit.
$ git bisect start c6 c0 Known good: c0 Known bad: c6 Selected version: c3 Test it, then record good or bad.
c0Known good boundaryc1Candidatec2Candidatec3Selected for testingc4Candidatec5Candidatec6Known bad boundary
Sample menu test
Test not run for this selected version.
Test the selected version before marking it. This local fixture has one regression on a linear history, with no later fix. Each recorded result narrows the good/bad interval; completion requires adjacent boundaries.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
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 skipif a commit is untestable. - Use
git bisect runto automate the search with tests.
Done with this concept?
Mark it complete to track your progress. No login needed.