Last Updated: February 25, 2016
·
1.832K
· lordofthejars

Git bisect to find a bug

Git bisect command command uses git to find in which commit a bug was introduced. After that, I am sure you will wonder where it's been you whole career.

The first thing to do is find one commit where the bug was not present and remember its hash (for ex. 1234). The next thing is find a commit where the bug was present and remember its hash (for ex. 5678).

And then we can start the search of the bug, type next commands in git repository:

git bisect start
git bisect good 1234
git bisect bad 5678

In this case we are setting to git that good commit was the one with hash 1234 and application is broken in hash 5678.

After that, git moves HEAD to a commit just in the middle between good and bad commits, and checkouts that version.

Next step is trying the checkout version and see if the bug was present or not. If it is not present then we execute:

git bisect good

on the other side if the bug was already introduced we must execute:

git bisect bad.

At this point git moves the HEAD in the middle between current commit and top or bottom of commits (depending if bug was already present or not). And this starts a recursive process (in fact git bisect uses binary search) until bug is found.

Finally git will show us the commit with the error, so we can inspect what was introduced. Finally we must execute:

git bisect reset

to leave HEAD pointing to last commit.

You can find an screencast in Git Bisect in Action

Thanks to @lightguard for showing me that command.

We keep learning,
Alex.

2 Responses
Add your response

i've never used bisce, but after reading your hot tip begin to love it :)
thanks for the info.

over 1 year ago ·

@drdrej you are welcome, thank you for comment. I agree with you, good command that automatize a bit the manual way of finding when a bug was introduced.

over 1 year ago ·