git bisect
Find the commit that introduced a bug by binary search
The 'git bisect' command finds the commit that introduced a bug. You mark one commit bad (has the bug) and one good (does not); git checks out the commit in the middle, you test it and answer good or bad, and git halves the suspects again until only one is left. Even a thousand commits take only about ten tests. git bisect reset brings you back to where you started.
Synopsis
git bisect start [<bad> [<good>...]]
git bisect (bad | good) [<rev>...]
git bisect skip [<rev>...]
git bisect reset
git bisect log
git bisect run <cmd>...Options
Choosing the Right Option
Tests needed
Binary search halves the suspects each time
| Command | bisect tests |
|---|---|
10 commits | about 4 |
100 commits | about 7 |
1000 commits | about 10 |
Examples
Start a session
git bisect startBegin the search.
Mark commits
git bisect bad && git bisect good v1.0Current commit is bad, v1.0 was good.
Automate
git bisect run npm testLet a test script answer for you.
Finish
git bisect resetBack to your branch.
Workflow Examples
git bisect start HEAD v1.0Bad now, good at v1.0
git bisect good / git bisect badTest each commit git checks out
git bisect resetBack to your branch
git revert <first-bad>Undo the culprit
Troubleshooting
You need to start by "git bisect start"
Cause: No session is running
Run git bisect start first
Wrong answer given
Cause: Every later step is based on it
git bisect log > log.txt, fix the line, git bisect reset, git bisect replay log.txt
Tips & Pitfalls
Best Practices
- ✓Write an automatic test and use git bisect run.
- ✓Always finish with git bisect reset.
Common Mistakes
- ✗Answering the wrong way once — every later step is then wrong (check with git bisect log).
- ✗Forgetting git bisect reset and continuing to work on a detached HEAD.
- ✗Starting with uncommitted changes that get in the way of checkouts.
FAQ
Yes: git bisect start --term-old=fast --term-new=slow, e.g. to find when something got slow.