Menu
Coddy logo textTech

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.

git bisect binary-searches history between a good and a bad commit, halving the suspect range at every step.

Syntax

CommandWhat it does
git bisect startBegin a bisect session
git bisect badMark the current commit as bad
git bisect good <hash>Mark a known-good commit
git bisect good / git bisect badMark each tested commit
git bisect run ./test.shAutomate: run a test to mark each step
git bisect resetEnd bisect, return to your branch

Worked example

Find which commit broke the build.

StepCommandResult
1git bisect startStart the search
2git bisect badCurrent commit is broken
3git bisect good v1.0This old tag worked
4Test, then good/badRepeat 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.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED