2 minutes
Using Git Bisect to Hunt Down Bugs
I was working on a project and I was making some changes to the code. I was not testing the code as I was making the changes, which was a bad idea (Don’t try this at home). After making some changes, I noticed that something broke.
I had no idea what caused the issue. I had made a lot of changes and I did not know where to start looking.
Linus Torvalds to the Rescue (Again)
I remembered that a friend of mine had told me about something called git bisect
.
I know what git bisect
does, but I had never used it before.
I decided to give it a try.
Using Git Bisect
The way git bisect
works is is uses a binary search algorithm
to find the commit that introduced a bug.
You simply give git bisect
a good commit and a bad commit,
Simply, the good commit is a commit where the code was working as expected, and the bad commit is a commit where the code is broken (the last commit in my case).
Here is how:
-
Start the bisect process:
git bisect start
-
Tell
git bisect
which commit is good and which commit is bad:git bisect good <good-commit>
git bisect bad <bad-commit>
-
git bisect
will checkout a commit in the middle of the range between the good and bad commits. -
Test the code to see if it is working as expected.
If the code is working as expected, tell
git bisect
that the commit is good:git bisect good
If the code is not working as expected, tell
git bisect
that the commit is bad:git bisect bad
-
git bisect
will checkout another commit in the middle of the range between the good and bad commits. -
Repeat steps 4 and 5 until
git bisect
finds the commit that introduced the bug. -
When
git bisect
finds the commit that introduced the bug, it will print the commit hash.
That is it!