Debugging with git bisect
4 learning objectives · 5 key terms · 5 commands · 1 diagrams · 5 quiz questions
Debugging with git bisect
Advanced GitWhat you'll be able to do
- Narrow any regression to its culprit commit with binary search
- Automate bisection with run scripts for hands-free hunting
- Use blame and grep to read history around suspects
- Recover cleanly from interrupted or mistaken bisections
The idea, in plain language
When something worked last week and fails today, git bisect binary-searches history for the breaking commit: you mark one good revision and one bad, then it checks out midpoints for you to test, halving the suspects each round — a thousand commits narrow in about ten tests. With git bisect run plus a test script, the whole hunt automates while you watch.
Blame and grep are the reading tools around it: git blame attributes each line to its commit (ignoring whitespace noise with -w, following moves with -C), and git grep searches tracked history faster than filesystem grep while respecting .gitignore. Together they turn "it broke sometime" into a named commit with an author, a diff, and a conversation to continue.
Key terms
- bisect
- Binary search over history between a known-good and known-bad commit.
- bisect run
- Automated bisection driven by a test script exit code (0 good, 1-124 bad, 125 skip).
- blame
- Line-by-line commit attribution for a file.
- grep (git)
- History-aware content search across tracked files at any revision.
- skip
- Marking an untestable commit so bisection routes around it.
How it works, step by step
- 1Bracket the breakage
git bisect start; git bisect bad HEAD; git bisect good v2.4.0 — one failing revision, one passing revision, and history between them.
bracketbashgit bisect start git bisect bad git bisect good v2.4.0 # Bisecting: 7 revisions left to test
- 2Test midpoints
Git checks out the middle; you run the reproduction and answer good or bad — each answer halves the field.
testbashnpm test # or: ./repro.sh # works? -> git bisect good # broken? -> git bisect bad
- 3Automate with run
git bisect run ./test.sh hands verdicts to exit codes (0 good, 1 bad, 125 skip) — the hunt completes unattended.
automatebashgit bisect run ./test.sh # ... a dozen checkouts flash by ... # a1b2c3d4 is the first bad commit
- 4Read around the culprit
git blame -w file attributes surviving lines; git log -S text finds when a string appeared — context for the fix and the conversation.
contextbashgit blame -w src/auth.py | head # a1b2c3d4 (Ada 2026-09-18 ...) token = request... git log -S "token =" --oneline
- 5Reset cleanly
git bisect reset returns to the original branch and state — run it even after findings, especially after interrupts.
resetbashgit bisect reset # back on main, bisect state cleared
Commands worth memorising
Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.
git bisect start && git bisect bad && git bisect good v2.4.0
Bisecting: 7 revisions left to test
Good must genuinely pass and bad genuinely fail, or every answer misleads.
git bisect run ./test.sha1b2c3d4 is the first bad commit
Exit 0 means good, 1-124 bad, 125 skip — the script contract is everything.
git blame -w src/auth.py
a1b2c3d4 (Ada 2026-09-18 10:02) token = request...
-w ignores whitespace-only churn so real authorship shows.
git grep "TODO" v2.4.0
(matches in that revision only)
Searches tracked files at any revision — faster and cleaner than filesystem grep.
git log -S "token =" --oneline
a1b2c3d4 Add token refresh
Lists commits that changed the count of matches — introduction and removal both appear.
Workflow diagrams
| Round | Remaining suspects | Action |
|---|---|---|
| Bracket | ~1000 commits | Mark good v2.4.0, bad HEAD |
| 1 | ~500 | Test midpoint, answer good/bad |
| 4 | ~60 | Keep halving |
| 10 | 1 | Culprit identified: a1b2c3d4 |
Logarithmic hunting: each answer discards half the remaining history.
- Good and bad brackets must both be honest or all answers mislead.
- Each verdict halves the field — ten rounds handle a thousand commits.
- Automation via run scripts removes the human bottleneck.
Worked example
A login regression hunted overnight by a script.
git bisect start git bisect bad HEAD git bisect good v2.4.0 git bisect run ./login-check.sh # a1b2c3d4 is the first bad commit git show a1b2c3d4 --stat git bisect reset
Culprit commit identified with its diff: a session-timeout change that dropped a token refresh.
Where you'll use it
Common mistakes
- Bracketing with a good that was never actually tested.
- Flaky test scripts whose verdicts mislead the search.
- Forgetting bisect reset and working from a detached midpoint.
- Blaming without -w and chasing whitespace-only commits.
Best practices
- Verify both brackets manually before automating.
- Make run scripts deterministic: exit 0 good, 1 bad, 125 skip.
- Reset after every session, findings or not.
- Follow culprits with blame and log -S for full context.
- Skip untestable commits explicitly rather than guessing.
Hands-on exercise
Hunt a planted regression
Introduce, then systematically find, a breaking change.
- In a practice repo, plant a subtle break several commits back.
- Bracket with verified good and bad revisions.
- Write a run script with 0/1/125 semantics and automate the hunt.
- Read the culprit with show, blame, and log -S.
- Reset cleanly and confirm normal state restored.
💡 Hint: Test the run script by hand on both brackets before trusting it unattended.
Done when: Named culprit commit with diff context, tree back to normal.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
How many tests does bisection need for ~1000 suspect commits?
- Multiple choiceQ2
What do git bisect run exit codes mean?
- True or falseQ3
True or false: you must run git bisect reset after finishing, even with findings in hand.
- TroubleshootingQ4
Bisection names a culprit you know is innocent. What went wrong first?
- Multiple choiceQ5
Why prefer git blame -w over plain blame when reading suspects?
Module summary
- Bisect binary-searches history between verified good and bad revisions.
- Run scripts automate verdicts via exit codes (0/1/125).
- Blame attributes lines; log -S finds string introductions.
- Always reset afterward — never work detached by accident.
- Honest brackets first; automation second.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.