Git Bisect
Last updated
git bisect finds the commit that introduced a bug by binary search. You tell it a known-good commit and a known-bad one; it checks out a commit halfway between, you mark it good or bad, and it narrows the range by half each time - so it pinpoints the culprit in a handful of steps even across thousands of commits.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git bisect start | Begin a bisect session |
git bisect bad | Mark the current commit as bad |
git bisect good <hash> | Mark a known-good commit |
git bisect good / git bisect bad | Mark each tested commit |
git bisect run ./test.sh | Automate: run a test to mark each step |
git bisect reset | End bisect, return to your branch |
Worked example
Find which commit broke the build.
| Step | Command | Result |
|---|---|---|
| 1 | git bisect start | Start the search |
| 2 | git bisect bad | Current commit is broken |
| 3 | git bisect good v1.0 | This old tag worked |
| 4 | Test, then good/bad | Repeat until Git names the commit |
Git bisect FAQ
What does git bisect do?
It performs a binary search through your commit history to find the exact commit that introduced a bug. You mark one commit as good and one as bad; Git repeatedly checks out the midpoint for you to test, halving the search range each time until it identifies the first bad commit.
How do I use git bisect?
Run
git bisect start, mark the current broken state with git bisect bad, and a known-working commit with git bisect good <hash>. Git checks out a midpoint; test it and run git bisect good or git bisect bad. Repeat until Git reports the first bad commit, then run git bisect reset.Can I automate git bisect?
Yes.
git bisect run <script> runs your script at each step - the script should exit 0 for a good commit and non-zero for a bad one (test runners work well). Git then bisects automatically without you marking each commit by hand, and prints the offending commit at the end.How do I finish a bisect?
Run
git bisect reset to end the session and return to the branch and commit you were on before you started. Do this whether or not you found the bad commit - it cleans up the temporary bisect state.Can I practice this online?
Yes. Open the terminal playground to run
git bisect in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers inspecting history step by step.