Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Last active May 10, 2021 10:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevenyap/8342d88260e6931cd50b to your computer and use it in GitHub Desktop.
Save stevenyap/8342d88260e6931cd50b to your computer and use it in GitHub Desktop.
Finding bugs in Git commits using git bisect

You have a git commit in your history that is causing a bug but you do not know which commit it is.
Here's how to use git bisect to find the commit that causes the bug.

# in the git root, start the git bisect
git bisect start

# mark current commit as bad
git bisect bad

# tell git which is the commit is good (it can be any good commit in the far history)
git bisect good <commit-hash>

# git will tell you roughly how many revisions you need to find the bad commit
# eg. Bisecting: 24 revisions left to test after this (roughly 5 steps)
# You are now at a commit that is chosen by git bisect
# Run your test to determine if the commit is okay

# if commit is okay, then
git bisect good

# if commit is bad, then
git bisect bad

# Repeat the above until git tells you which is the bad commit
# eg. f5836a61c5e989b972499e5265760519580d3cfe is the first bad commit
# Note down your bad commit hash

# Upon finishing, then
git bisect reset

# Work on your bad commit!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment