How to Find When a Bug Was Introduced in Git Using Git Bisect
Use
git bisect to find when a bug was introduced by performing a binary search through your commit history. Mark a known good commit and a bad commit, then test each step until Git pinpoints the exact commit that caused the bug.Syntax
The basic git bisect workflow involves these commands:
git bisect start- Begin the bisect session.git bisect bad- Mark the current commit as bad (where the bug exists).git bisect good <commit>- Mark a known good commit where the bug did not exist.- Test the commit Git checks out and mark it as
goodorbadaccordingly. git bisect reset- End the bisect session and return to the original branch.
bash
git bisect start # Mark current commit as bad git bisect bad # Mark known good commit git bisect good <commit> # Repeat testing and marking commits as good or bad # After bisect finishes git bisect reset
Example
This example shows how to find the commit that introduced a bug using git bisect. Suppose the bug is present in the current commit, but not in commit abc1234.
bash
git bisect start
# Current commit has the bug
git bisect bad
# Known good commit without the bug
git bisect good abc1234
# Git will checkout a commit in the middle
# Test if bug is present
# If bug is present:
git bisect bad
# If bug is not present:
git bisect good
# Repeat testing until Git finds the first bad commit
# When done, reset to original branch
git bisect resetOutput
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[def5678] Fix typo in README
Bisecting: 1 revision left to test after this (roughly 1 step)
[ghi9012] Add new feature
First bad commit found: [ghi9012] Add new feature
Common Pitfalls
- Not marking commits correctly: Always mark commits as
goodorbadbased on testing results to guide Git. - Using an incorrect good commit: The good commit must be before the bug was introduced, or bisect will fail.
- Not resetting after bisect: Always run
git bisect resetto return to your original branch. - Automating tests: For large projects, use
git bisect run <script>to automate testing.
bash
## Wrong way: Marking bad commit as good git bisect start git bisect bad git bisect good <commit_with_bug> # This will cause bisect to fail ## Right way: Mark a commit without the bug as good git bisect start git bisect bad git bisect good <commit_without_bug>
Quick Reference
| Command | Description |
|---|---|
| git bisect start | Start bisect session |
| git bisect bad | Mark current commit as bad (bug present) |
| git bisect good | Mark known good commit (bug absent) |
| git bisect reset | End bisect and return to original branch |
| git bisect run | Automate bisect with test script |
Key Takeaways
Use git bisect to quickly find the commit that introduced a bug by binary searching your commit history.
Always mark commits as good or bad based on testing results to guide the bisect process.
Choose a known good commit before the bug appeared to start bisect correctly.
Run git bisect reset after finishing to return to your original branch.
Automate testing with git bisect run for faster bug identification in large projects.