Do you know which Git command often flies under the radar, despite its incredible usefulness? Enter Git Bisect - the unsung hero of git. 🚀
In this article we will cover this underdog tool, revealing how it simplifies hunting down bugs. ✨
When to use Git Bisect? 🤔
Git Bisect is like a detective for your code! When your project has a bug and you're not sure which change caused it, Git Bisect helps you find the sneaky culprit by narrowing down the bad commit/code step by step. 😲
Super useful when you want to track down that sneaky bug hiding in your project! 🐛
🔴 But let me make it clear,
git bisect
is not a git command you will use very often. Nonetheless, it's one of the most helpful and impressive Git commands.
Basic git bisect
workflow 🎯
💡What
git bisect
does is, it divides the revision commits into a good part and a bad part by testing specific commits chosen by a binary search.
We will not go through every other nitty-gritty detail in this article. For more information visit here.
You start by saying, "Hey Git, let's find that bug!" -
git bisect start
Git checks out all the commits where the bug is there and where it's not. -
git bisect good/bad
You check and tell Git if the bug is still there. -
Continue...
Git keeps showing you commits until you catch the sneaky bug. Bug busted! 🐛🎉 -
git bisect reset
Hands On! ⚒️
Let's implement our little knowledge of what we just learned about git bisect
and its basic workflow.
Scenario 🎥
My blog site was running smoothly until a few days back, but then something went wrong with a recent update. 🥲
But here we can use the magic of the git bisect to find out which commit caused the issue.
First, let’s run the git log --oneline
command to see the commit history:
Here the bug introducing commit is
690a8a0
, but let's pretend we don't know which commit introduced the error. 🤡
For the first good commit, we will pick 5e2bae3
. As far as we know, that’s the last commit that doesn’t have the error.
For the first bad commit, we can choose the latest commit 464c3c3
since the app is not currently working. ☹️
Now, let's start git bisect
and provide the initial good and bad commits.
Now, git checks out every other commit in the commit revision. Now, we need to test our app manually on every checkout. The commit we're currently checked out cdd57cc
seem to not have the issue. So, we mark it good. ✅
Now, we are only left with these two commits - 690a8a0
c2a0519
Git now checks out the buggy commit 690a8a0
which on testing manually seem to have caused the issue. Finally, we have found that buggy🐛 commit.
Once you’ve finished your testing, you can reset bisect using
git bisect reset
.
That is it for this article! 😎
Here, we covered the manual git bisect
, but if you have a test script that you can run to validate commits, you can use it to automate the whole thing with the command git bisect run <script>
Feel free to follow me to get notified when new articles are out 😉
Let's stay connected👋
- GitHub: https://github.com/shricodev
- LinkedIn: https://linkedin.com/in/shrijal007
See you next time✌️