0
0
GitHow-ToBeginner · 3 min read

How to Use Git Bisect to Find Bugs Quickly

Use git bisect start to begin, mark a known bad commit with git bisect bad, and a known good commit with git bisect good. Git will then check out commits between these points for you to test, helping you quickly find the commit that introduced a bug.
📐

Syntax

The basic git bisect commands are:

  • git bisect start: Begin the bisect session.
  • git bisect bad [commit]: Mark the current or specified commit as bad (buggy).
  • git bisect good [commit]: Mark the current or specified commit as good (working).
  • git bisect reset: End bisect and return to the original branch.

Git uses these points to perform a binary search through commits.

bash
git bisect start
# Mark the current commit as bad
git bisect bad
# Mark a known good commit
git bisect good <commit-hash>
# After testing each commit, mark it good or bad
# Repeat until the bad commit is found
# End bisect session
git bisect reset
💻

Example

This example shows how to find the commit that introduced a bug between the current commit and a known good commit.

bash
git bisect start
# Current commit is bad
git bisect bad
# Mark a known good commit by its hash
git bisect good a1b2c3d4

# Git checks out a commit in the middle for testing
# Test your code here
# If bug is present:
git bisect bad
# If bug is not present:
git bisect good

# Repeat testing and marking until git finds the first bad commit

# When done, reset to original branch
git bisect reset
Output
Bisecting: 3 revisions left to test after this (roughly 2 steps) [commit_hash] Commit message # ... Bisecting: 0 revisions left to test after this (roughly 0 steps) [bad_commit_hash] Commit message # first bad commit found
⚠️

Common Pitfalls

Common mistakes when using git bisect include:

  • Not marking the correct commits as good or bad initially, which leads to incorrect results.
  • Forgetting to test the checked-out commit before marking it.
  • Not resetting after finishing, leaving the repo in a detached HEAD state.
  • Using git bisect bad or git bisect good without testing the current commit.

Always test the code at each step before marking it.

bash
git bisect start
# Wrong: marking bad without testing
git bisect bad
# Correct: test first, then mark
# (run tests or check bug)
git bisect bad
📊

Quick Reference

CommandDescription
git bisect startStart bisect session
git bisect bad [commit]Mark commit as bad (buggy)
git bisect good [commit]Mark commit as good (working)
git bisect resetEnd bisect and return to original branch
git bisect logShow bisect steps taken
git bisect replay Replay bisect session from log

Key Takeaways

Start bisect with known good and bad commits to narrow down the bug.
Test each checked-out commit before marking it good or bad.
Use git bisect reset to return to your original branch after finishing.
Incorrect marking leads to wrong results, so be careful with good/bad commits.
Git bisect uses binary search to find the first bad commit efficiently.