How do I use git bisect?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
git bisect is the fastest way to locate the commit that introduced a bug when you know one older commit was good and a newer commit is bad. It uses binary search over your commit history, so instead of checking dozens of commits one by one, you usually test only a small handful.
The Basic Workflow
A bisect session needs two anchor points:
- one known bad commit, often
HEAD - one known good commit, where the bug definitely does not exist
Then Git checks out a midpoint commit for you to test. After each test, you tell Git whether that revision is good or bad, and Git picks the next midpoint.
A typical session looks like this:
Here, HEAD is assumed to be bad, and tag v1.4.2 is known good. Git now checks out a candidate commit in detached HEAD state.
Testing Each Candidate Commit
At each step, run the smallest reliable test that exposes the bug. That could be a unit test, a command-line invocation, or even a manual reproduction if automation is impossible.
If the bug is present:
If the bug is absent:
Git repeats that process until it narrows the search to one commit. A final message identifies the first bad commit, along with the commit message and hash.
This process is efficient because every answer cuts the search space roughly in half.
Automate The Test When Possible
Manual bisecting works, but an automated test is much better because it removes guesswork and speeds up the whole search.
Create a script that exits with 0 for good and nonzero for bad. Then let Git run it for each midpoint:
git bisect run checks out each midpoint, runs the script, reads the exit status, and continues automatically.
This is the best approach when the bug is reproducible in CI-like conditions.
Picking Good And Bad Commits Carefully
The search is only as good as your anchor commits. If the so-called good commit already contains the bug, or the bad commit fails for an unrelated reason, the result becomes misleading.
Pick commits with these properties:
- the repository builds successfully
- the test condition is stable
- the bug is clearly present or clearly absent
If some intermediate commits are broken for unrelated reasons, you can skip them:
Skipping is useful when a midpoint cannot be tested because of a build failure unrelated to the bug you are chasing.
A Realistic Example
Assume your application crashes on startup today, but worked in release v2.0.0.
Git checks out a midpoint. You run:
If the startup test fails, mark that midpoint bad. If it passes, mark it good. After several iterations, Git reports the first bad commit. At that point, inspect the diff:
The point of git bisect is not only to identify who changed the code, but to reduce the debugging surface area to one exact change.
Always Reset When You Finish
A bisect session leaves the repository in detached HEAD state. When you are done, restore your normal checkout:
This returns you to the branch you were on before the bisect started.
If you forget this step, later commands can feel confusing because you are no longer on your working branch.
Common Pitfalls
The biggest pitfall is using an unreliable test. If the test is flaky, Git may classify the same commit differently on different runs, which undermines the search.
Another common mistake is picking a good commit that was never actually verified. A label like old release is not enough unless you know the specific bug was absent there.
People also forget about uncommitted changes. Start from a clean working tree so your local modifications do not affect the test result.
Finally, remember that git bisect only finds the first bad commit relative to your chosen good point. If the bug depends on multiple later changes or environmental drift, you may still need follow-up analysis.
Summary
- '
git bisectperforms a binary search through commit history.' - Start with one known bad commit and one known good commit.
- Test each midpoint and mark it
good,bad, orskip. - Prefer
git bisect runwith an automated script whenever possible. - Finish with
git bisect resetto return to your normal branch.
Related reading
- How do I use 'git reset --hard HEAD' to revert to a previous commit?
- How do I use 'git reset --hard HEAD' to revert to a previous commit?
- How do I use Git's difftool to merge conflicts?
- How do I use Notepad or other with msysgit?
- How do I use sudo to redirect output to a location I don't have permission to write to?
- How do I view events fired on an element in Chrome DevTools?
- How do I view 'git diff' output with my preferred diff tool/ viewer?
- How do I view 'git diff' output with my preferred diff tool/ viewer?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.