What is Git bisect and how to use this tool to find a bug in the code?
The git bisect tool is designed for automatic or semi-automatic searching of the commit that introduced a bug in the code. When an application suddenly stops working and you do not know which specific change broke the functionality, this method becomes a real lifesaver. Instead of manually checking every change, git bisect uses a binary search algorithm.
To start working with the utility, you need to put the repository into search mode and provide the program with two starting points. First, you need to tell Git the current state where the bug is guaranteed to be present using the git bisect bad command. Then, specify the last known working commit where the problem did not yet exist via the git bisect good command and the hash of that stable commit.
After initialization, Git will automatically switch the HEAD pointer to a commit right in the middle between the good and bad points. Now the developer's task is to test the application in this state. If the code works correctly and there is no bug, git bisect good is entered into the terminal. If the bug is present, git bisect bad is entered.
The system will continue to divide the remaining range of commits in half after each of your answers. This process is repeated several times depending on the total number of changes being checked. Usually, checking hundreds of commits takes only seven to ten iterations.
As soon as the binary search is completed, Git will display the exact hash and description of the problematic commit, as well as the name of the author who created it. To exit the search mode and return to the original branch, simply execute the git bisect reset command. This will clear temporary files and restore your working environment.