Learn GitHub
$ gh ghadvancedgit.ghbisect

Debugging with git bisect

4 learning objectives · 5 key terms · 5 commands · 1 diagrams · 5 quiz questions

$objectives

Debugging with git bisect

Advanced Git

Module 3 of 5 · lesson 12 in this module

What 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
$concept

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

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

How it works, step by step

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

    bracketbash
    git bisect start
    git bisect bad
    git bisect good v2.4.0
    # Bisecting: 7 revisions left to test
  2. 2
    Test midpoints

    Git checks out the middle; you run the reproduction and answer good or bad — each answer halves the field.

    testbash
    npm test  # or: ./repro.sh
    # works? -> git bisect good
    # broken? -> git bisect bad
  3. 3
    Automate with run

    git bisect run ./test.sh hands verdicts to exit codes (0 good, 1 bad, 125 skip) — the hunt completes unattended.

    automatebash
    git bisect run ./test.sh
    # ... a dozen checkouts flash by ...
    # a1b2c3d4 is the first bad commit
  4. 4
    Read 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.

    contextbash
    git blame -w src/auth.py | head
    # a1b2c3d4 (Ada 2026-09-18 ...) token = request...
    git log -S "token =" --oneline
  5. 5
    Reset cleanly

    git bisect reset returns to the original branch and state — run it even after findings, especially after interrupts.

    resetbash
    git bisect reset
    # back on main, bisect state cleared
$commands

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.

bracket the regressionbash
git bisect start && git bisect bad && git bisect good v2.4.0
Output
Bisecting: 7 revisions left to test

Good must genuinely pass and bad genuinely fail, or every answer misleads.

automate the huntbash
git bisect run ./test.sh
Output
a1b2c3d4 is the first bad commit

Exit 0 means good, 1-124 bad, 125 skip — the script contract is everything.

blame a linebash
git blame -w src/auth.py
Output
a1b2c3d4 (Ada 2026-09-18 10:02) token = request...

-w ignores whitespace-only churn so real authorship shows.

search history contentbash
git grep "TODO" v2.4.0
Output
(matches in that revision only)

Searches tracked files at any revision — faster and cleaner than filesystem grep.

find string introductionbash
git log -S "token =" --oneline
Output
a1b2c3d4 Add token refresh

Lists commits that changed the count of matches — introduction and removal both appear.

$diagrams

Workflow diagrams

$example

Worked example

A login regression hunted overnight by a script.

ten rounds while you sleepbash
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
Output
Culprit commit identified with its diff: a session-timeout change that dropped a token refresh.
1Honest brackets: v2.4.0 demonstrably logs in, HEAD demonstrably does not.
2The script exits 0/1 on login success/failure — the entire verdict contract.
3Dawn reveals one commit; show plus blame supply author, diff, and context for the fix.
4Reset returns the tree to normal — bisection state never lingers.
$use cases

Where you'll use it

▸Regressions with unknown introduction points in long histories.
▸Overnight automated hunts via run scripts.
▸Attributing suspicious lines with blame before editing.
▸Finding when strings appeared or vanished with log -S.
$mistakes

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

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

Hands-on exercise

Hunt a planted regression

Introduce, then systematically find, a breaking change.

  1. In a practice repo, plant a subtle break several commits back.
  2. Bracket with verified good and bad revisions.
  3. Write a run script with 0/1/125 semantics and automate the hunt.
  4. Read the culprit with show, blame, and log -S.
  5. 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.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    How many tests does bisection need for ~1000 suspect commits?

  2. Multiple choiceQ2

    What do git bisect run exit codes mean?

  3. True or falseQ3

    True or false: you must run git bisect reset after finishing, even with findings in hand.

  4. TroubleshootingQ4

    Bisection names a culprit you know is innocent. What went wrong first?

  5. Multiple choiceQ5

    Why prefer git blame -w over plain blame when reading suspects?

$summary

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.

Next up: History debugged; next is letting AI pair with you — Copilot-assisted professional workflows.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

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.