By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
You’re working on a large codebase, and suddenly, a feature that worked yesterday is broken today. The commit history is long, and you have no idea where the bug was introduced. Git bisect is your time machine—it helps you binary search through commits to find the exact moment the bug appeared.
Why this matters in production:- Saves hours of manual debugging. Instead of checking every commit, git bisect narrows it down in logarithmic time.- Critical for legacy codebases. If you inherit a project with hundreds of commits, bisecting is the fastest way to isolate regressions.- Prevents "shotgun debugging." No more randomly reverting commits—you get proof of which change broke things.
git bisect
Real-world scenario:You’re a DevOps engineer maintaining a CI/CD pipeline. A test suite that passed last week now fails. The team suspects a recent merge, but no one knows which commit introduced the issue. Git bisect lets you pinpoint the exact commit in minutes, so you can revert or fix it before the next deployment.
git bisect – A Git command that performs a binary search through commit history to find the first "bad" commit. Production insight: If you don’t use bisect, you’re either guessing or manually checking commits—both are slow and error-prone.
git bisect start – Begins a bisect session. Production insight: Always start bisect in a clean working directory (no uncommitted changes).
git bisect start
git bisect good [commit] – Marks a commit as working (no bug). Production insight: If you mark the wrong commit as "good," bisect will give incorrect results.
git bisect good [commit]
git bisect bad [commit] – Marks a commit as broken (contains the bug). Production insight: If you mark the wrong commit as "bad," bisect may miss the real culprit.
git bisect bad [commit]
git bisect reset – Exits bisect mode and returns to the original branch. Production insight: Always reset after bisecting to avoid confusion.
git bisect reset
git bisect skip – Skips a commit if it can’t be tested (e.g., missing dependencies). Production insight: Use sparingly—skipping too many commits reduces accuracy.
git bisect skip
git bisect run [script] – Automates bisecting by running a test script. Production insight: The script must exit with 0 (success) or 1-127 (failure). Non-zero means "bad."
git bisect run [script]
0
1-127
Binary search – The algorithm behind bisect, halving the search space each time. Production insight: Bisect is O(log n), meaning 1000 commits take ~10 steps, not 1000.
✅ A Git repository with multiple commits.✅ A bug that exists in the latest commit but not in an older one.✅ A way to test for the bug (manual check or script).
Expected output:
Bisecting: X revisions left to test after this (roughly Y steps)
Find a commit where the bug does not exist (e.g., git log --oneline to browse history).
git log --oneline
git bisect good abc1234
Example:
git bisect good 3a4b5c6
Bisecting: 50 revisions left to test after this (roughly 6 steps)
Usually, the latest commit (HEAD) is bad.
HEAD
git bisect bad HEAD
Bisecting: 25 revisions left to test after this (roughly 5 steps)
Git automatically jumps to a commit halfway between good and bad.Example:
good
bad
HEAD is now at def7890... Fix login button styling
Run your test (manual or automated).- If the bug exists, mark it as bad: bash git bisect bad - If the bug does not exist, mark it as good: bash git bisect good - If you can’t test (e.g., missing dependencies), skip it: bash git bisect skip
bash git bisect bad
bash git bisect good
bash git bisect skip
Git keeps narrowing down commits until it identifies the first bad one.Example final output:
abc1234 is the first bad commit commit abc1234 Author: Jane Doe <[email protected]> Date: Mon Jan 1 12:00:00 2024 +0000 Refactor user authentication
Previous HEAD position was def7890... Fix login button styling Switched to branch 'main'
git show abc1234
What to look for: - Code changes that could introduce the bug.- Tests that might have failed.
git bisect run
Automate with git bisect run. Example: bash git bisect start HEAD abc1234 git bisect run ./test-script.sh Why? Manual bisecting is slow—automate it if you can.
bash git bisect start HEAD abc1234 git bisect run ./test-script.sh
Skip untestable commits. If a commit can’t be built (e.g., missing dependencies), use git bisect skip.
git bisect log
bash git bisect log
git bisect start # bad: [def7890] Fix login button styling git bisect bad def7890 # good: [abc1234] Refactor user authentication git bisect good abc1234
bash git tag bug-introduced abc1234
git show
❌ git bisect begin (doesn’t exist)
git bisect begin
"How do you mark a commit as bad?"
git bisect bad
❌ git bisect broken (invalid)
git bisect broken
"What does git bisect run do?"
❌ Runs a Git hook (incorrect).
"What happens if you don’t reset after bisecting?"
git bisect good
skip
reset
"A bug was introduced between commit abc123 (good) and def456 (bad). How do you find the first bad commit?" - ✅ git bisect start def456 abc123 → test → mark good/bad → repeat.- ❌ git blame (only shows line changes, not commit history).
abc123
def456
git bisect start def456 abc123
git blame
Challenge:You have a Git repo with 100 commits. A bug exists in HEAD but not in commit abc123. Use git bisect to find the first bad commit in 7 steps or fewer.
Solution:
git bisect start HEAD abc123 git bisect run ./test-script.sh # Assuming test-script.sh exits 0 (good) or 1 (bad)
Why it works: git bisect run automates the binary search, halving the search space each time.
git bisect good abc123
git bisect run ./test.sh
Final Pro Tip:Bisecting is not just for bugs—use it to find: - Performance regressions (e.g., "When did this query get slow?").- Breaking changes in APIs.- Missing features ("When was this button removed?").
Now go bisect like a pro. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.