Skip to content

Martin Belev

How to find an old introduced bug using Git - git bisect introduction?

Git1 min read

git bisect

Want to find a bug that was introduced some time ago?

We can use binary search using Git to find the culprit.

How?

The answer is - git bisect.

Let's dive into an introduction and see how we can use it.

Link to this heading
Where to start from?

You've seen something is broken with the latest changes on your development branch and want to find to the problem.

First, you need to find an old commit where everything was working as expected.

Then proceed with executing the following command:

1git bisect start

Link to this heading
Bad commit

Almost nothing is being done at this point.

Now you need to tell Git which is the bad commit. Most probably just going to be HEAD/latest commit.

Execute the following command:

1git bisect bad HEAD

You could use commit hash as well.

Link to this heading
Good commit

Then comes the part where we need to tell Git which is the good commit where everything was working fine.

Execute the following command:

1git bisect good <commit-hash>

Link to this heading
Bisect has been started

Awesome - you've started the bisecting now and should see something similar to:

1Bisecting: 157 revisions left to test after this (roughly 7 steps)
2[<commit-hash>] <commit-message>

You are now moved to a commit somewhere in the middle between the bad and good commit.

Link to this heading
What information is Git giving us?

157 revisions - showing the number of commits between the bad and the good commit.

roughly 7 steps - showing you how many steps you will need until the end.

[<commit-hash>] <commit-message> - showing at which commit you are currently at.

Link to this heading
Proceeding further

Test the commit and based on the result proceed further.

  • If the problem is not gone execute - git bisect bad
  • If it's working execute - git bisect good

You will be moved to the next commit for testing.

Repeat the process until there are no more steps and commits to check.

In the end, Git will output the first bad commit where the problem was introduced.

Link to this heading
Conclusion

Hopefully, it was useful and you have a new tool in your arsenal now to tackle a problem using Git 💪.

Thank you for reading this to the end 🙌 . If you enjoyed it and learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter where we can catch up. I am sharing some other tips, articles, and things I learn there.
If you didn't like the article or you have an idea for improvement, please reach out to me on Twitter and drop me a DM with feedback so I can improve and provide better content in the future 💪.

© 2021 by Martin Belev. All rights reserved.