Git Notes
Learn how to use git bisect to efficiently find the commit that introduced a bug using binary search. Master automated bisect with scripts and practical debugging workflows.
Git bisect uses binary search to efficiently find the commit that introduced a bug. Instead of checking every commit linearly, bisect cuts the search space in half with each step, finding the problematic commit in logarithmic time.
How Bisect Works
Basic Bisect Workflow
Step 1: Start Bisect
git bisect startStep 2: Mark the Bad Commit (Has the Bug)
# Current commit has the bug
git bisect bad
# Or specify a specific commit
git bisect bad HEAD
git bisect bad abc1234Step 3: Mark a Good Commit (No Bug)
# Specify a commit where the bug did NOT exist
git bisect good v1.0
# or
git bisect good a1b2c3dBisecting: 25 revisions left to test after this (roughly 5 steps) [f4e3d2c1b0a9] Add user authentication module
Step 4: Test Each Commit
Git checks out a commit in the middle. Test it and mark accordingly:
# If the bug IS present at this commit
git bisect bad
# If the bug is NOT present at this commit
git bisect goodBisecting: 12 revisions left to test after this (roughly 4 steps) [e3d2c1b0a9f8] Refactor database queries
Step 5: Repeat Until Found
Continue marking commits as good or bad until Git identifies the first bad commit:
e3d2c1b0a9f8e7d6c5b4a3 is the first bad commit
commit e3d2c1b0a9f8e7d6c5b4a3
Author: John Doe <john@example.com>
Date: Mon Jan 15 14:30:00 2024
Refactor database queries
src/db/queries.js | 45 ++++++++++++++++++++++++---------------------
1 file changed, 24 insertions(+), 21 deletions(-)Step 6: End Bisect
git bisect resetPrevious HEAD position was e3d2c1b... Refactor database queries Switched to branch 'main'
Complete Example
# Bug: Login page crashes on submit (works in v2.0, broken now)
git bisect start
git bisect bad HEAD # Current state has the bug
git bisect good v2.0 # v2.0 was working fineBisecting: 50 revisions left to test after this (roughly 6 steps) [abc1234] Update form validation
# Test the application at this commit
npm test # or manually test
# Tests pass - this commit is fine
git bisect goodBisecting: 25 revisions left to test after this (roughly 5 steps) [def5678] Add new login dependencies
# Test again - this one fails!
npm test
git bisect badBisecting: 12 revisions left to test after this (roughly 4 steps)
# Continue until found...
git bisect good # (after testing)
git bisect bad # (after testing)
git bisect good # (after testing)def5678abc is the first bad commit
commit def5678abc
Author: Jane Smith <jane@example.com>
Date: Wed Jan 17 09:15:00 2024
Add new login dependencies
package.json | 3 +++
src/login.js | 8 ++++++++
2 files changed, 11 insertions(+)# Done! Reset to go back to where you were
git bisect resetAutomated Bisect with Scripts
Instead of manually testing each commit, provide a script:
git bisect start HEAD v2.0
git bisect run npm testrunning 'npm test' ... Tests passed Bisecting: 25 revisions left... running 'npm test' ... Tests FAILED ... abc1234 is the first bad commit bisect run success
Custom Test Script
# Create a test script
cat > /tmp/test-bisect.sh << 'EOF'
#!/bin/bash
# Return 0 (good) if test passes, non-zero (bad) if it fails
make build && ./run-specific-test
EOF
chmod +x /tmp/test-bisect.sh
# Run automated bisect
git bisect start HEAD v1.0
git bisect run /tmp/test-bisect.shExit Codes for bisect run
| Exit Code | Meaning |
|---|---|
| 0 | Good commit (no bug) |
| 1-124, 126-127 | Bad commit (has bug) |
| 125 | Skip (cannot test this commit) |
| 128+ | Abort bisect |
Handling Untestable Commits
# If a commit cannot be compiled or tested
git bisect skipBisect with Specific Terms
Use custom terms instead of good/bad:
# Find when a performance regression was introduced
git bisect start --term-old=fast --term-new=slow
git bisect slow HEAD
git bisect fast v1.0
# Mark commits
git bisect fast # This commit is fast (no regression)
git bisect slow # This commit is slow (has regression)Bisect Log and Replay
View Bisect Progress
git bisect loggit bisect start # status: waiting for both good and bad commits git bisect bad abc1234 # status: waiting for good commit git bisect good def5678 # status: bisecting git bisect good ghi9012 git bisect bad jkl3456
Save and Replay a Bisect Session
# Save the log
git bisect log > bisect-session.log
# Replay later
git bisect replay bisect-session.logPractical Tips
Bisect with Merge Commits
# Skip merge commits that might not build
git bisect start
git bisect bad HEAD
git bisect good v1.0
# If a merge commit doesn't build:
git bisect skipCombine with git log
# After finding the bad commit, investigate
git show <bad-commit>
git log --oneline <good-commit>..<bad-commit>Interview Questions
- What is git bisect and how does it work?
Git bisect uses binary search to find the commit that introduced a bug. You mark a known good and bad commit, and Git repeatedly checks out the middle point for testing, halving the search space each time until the exact commit is found.
- How does automated bisect work?
Use git bisect run <script>. The script must return exit code 0 for good commits and non-zero for bad commits. Git automatically marks each tested commit based on the script result.
- What exit code 125 means in bisect run?
Exit code 125 tells Git to skip the current commit (it cannot be tested, perhaps because it does not compile). Git will try an adjacent commit instead.
- How many steps does bisect take to find a bug in 1000 commits?
Approximately log2(1000) ≈ 10 steps. Each step halves the remaining commits, making bisect extremely efficient for large histories.
- How do you go back to normal after bisecting?
Run git bisect reset. This returns HEAD to where it was before bisect started and cleans up the bisect state.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Bisect - Find Bugs with Binary Search.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, advanced, bisect, git bisect - find bugs with binary search
Related Git & GitHub Topics