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.
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.