Find the exact commit that has issue

Yesterday I was working on a repository and I have made more than 30 commits on working branch. On my tests, I encountered an issue and after a quick look, I couldn’t find the commit with the issue.

My first option would be to use a debugger but I was going to see in which commit have I made the mistake. If I wanted to do it the wrong way, It would be to go back commit by commit to see where the error is. However, this would take a lot of time and I was sure that there must a better way.

What is a binary search?

Binary search is an algorithm that works on sorted arrays. Imagine that you have an array of numbers like this:

[2, 4, 5, 7, 9, 12, 15, 17, 20, 32, 37]

If you want to search for a number in this array, it would be inefficient to compare each number in the array one by one. A binary search, divides the array into 2 parts and compare your value to middle value of the array.

[2, 4, 5, 7, 9, 12, 15, 17, 20, 32, 37]

If your value is bigger than the middle one, then it would do the same thing for the upper half of the array and repeat the same steps. If your value is smaller, then it would go to the bottom half of the array and would try to divide it in half and repeat the same steps.

Let’s say in the above array we’re looking for number 9. The first step after dividing the array, would be to compare 12 to 9. Since it’s smaller, we need to then to do the steps for the fist half of the array:

[2, 4, 5, 7, 9]

Now when we compare 9 to 5, we see that it’s bigger and then we need to do the same thing for the second half of the remaining array and fine the value we want.

Find the commit with the issue

Git bisect is a command that finds the commit that has caused a certain issue on your website. How does it work? You simply tell the command what is the latest command that you think was without the issue (as the good commit) and what was the latest commit with the issue(as the bad commit).

To start the process, you need to start with this command:

git bisect start

This will start the process and until you finish the process, you’re in bisect. Now consider the array example in the binary search section. The array has a first value and last value. The first value here is your good commit and the last value is your bad commit. You need to specify these commits in the process like this:

git bisect bad <commit-hash>

And then after this, you need to identify the good commit:

git bisect good <commit-hash>

If you don’t know what is your latest good commit, just take a good guess and you’ll be good. If you don’t know what is your commit hash, the easiest way is to use git log command and see the list of your commits and their hashes.

After telling the bisect what are your good and bad commits, you’re now in the process. The bisect will take you to the middle of the commits and the project will be at that step. All you need to do is to test your project to see if it still has the issue or now.

If it still has the issue, you should flag this commit is bad like this:

git bisect bad

And if the project doesn’t have the issue, you need to flag it as good:

git bisect good

If the commit was bad, the bisect will again take you to another commit and you need to do the same things and test the project. After it finds the commit with the issue, it will show the commit information and you can further investigate the issue.

Don’t forget the finish the process by running:

git bisect reset

Debugging in WordPress

Basically the best option to debug WordPress issues is to use the default WordPress debugger. This debugger shows you the error on your website and it will be useful if you have an actual PHP error. If you have JS errors, the best option would be to check your console and if you don’t have any visible errors, it would be better to use a debugger in your code editor.

Click here to see more articles on debugging.




Comments

Leave a Reply

Your email address will not be published. Required fields are marked *