Hunting bugs like a pro
Daniel Afonso —
Whether we like it or not, bugs are a part of every developer’s day-to-day. As a developer, we can spend countless hours tracking out where the issue originated. Sometimes you may be lucky. Other times after countless lost hours and hairs, you may end up giving up and probably think about quitting your job.
What if I told you that git gives you the possibility of using binary search to find the commit that introduced a bug by using a fantastic command called
So let us picture that we find a bug in our current branch, we can start our bisecting now. To do so, run the
git bisect start command.
Since we have the bug in this commit we need to mark it as a bad commit. To do so, run the command
git bisect bad.
Now, it’s the part we need to do some research.
We need to look into our previous commits and find one where there was no bug.
<commit_id> is a commit where there is no bug, here we need to run the command
git bisect good <commit_id>.
Now that we identified a good and bad commit, git will start by checking out other commits. Now you can run your project and see if the bug persists on this commit. If it does then run the
git bisect bad command, if not then run
git bisect good.
Now repeat this process on every commit that git does a checkout on. If everything goes well, at the end git will identify what was the commit that first introduced that bug.Finding the first bad commit
Now we finally know where the bug first showed up. Using this information we, can check that commit and track the bug easier.
As you can see, if you follow a good approach with version control, you can leverage git to track down bugs in an easy way without much effort and save you tons of time in the process.
Finally, I wish you all a fantastic 2022 and that you achieve all your goals! I hope you enjoyed this blog post and, stay tuned for the next ones!