You’ve been working on a branch for the past 3 days. 30 commits later you are ready to complete your feature when all of a sudden. You realize that something is wrong with the footer of your application. As you panic, you look at your previous commits to see if anything has changed in the footer. In fact, the footer has not been touched and you have no idea what’s going on.
git bisect you could save countless hours of trying to find the exact commit that introduced the bug, without having to go commit by commit.
git bisect will perform a binary search to help you find the exact commit you’re looking for. If you are unfamiliar with a binary search it basically means it will divide the options in half each time as you answer a simple question Does the commit that it is showing you have the bug or not.
Now, to start the process use
git bisect start. After doing this nothing happens... What you then need to do is supply git with a commit that you know where the bug not present or a ‘good’ commit
git bisect good ch4792h2 for example. Then enter the second commit which has a known instance of the app with the bug, in other words, a ‘bad commit’ such as
git bisect bad ke37lw5
After doing this git will checkout a commit for you that you will then have to respond with
git bisect good if the commit it's showing you does not have the bug or
git bisect bad if it does. After answering, git will narrow the search by removing the other half of the commits, then asking you the same question. Eventually narrowing down to the commit that introduced the bug.
And that’s it! In about 4 to 5 commits you were able to find the commit that introduced the bug. Now, this might be more beneficial for checking for changes in the UI since you can clearly see when something in the app is not working like its supposed to. However, it is a nice command to remember from time to time if something ever comes up
Claim your page on DEV before someone else does
Level up every day