| by Arround The Web | No comments

Git Bisect: Efficiently Debugging Code Regressions

Debugging for the bugs in the code is a difficult task especially when you have thousands of line codes and files in a project. In a developer’s life, there are countless scenarios where the specific feature of the code was working in the previous release. While in the new version, it stopped working. In such situations, Git offers the tool named bisect that permits the user appearance of the particular bug in the project release.

The outcome of this post is:

How to Debug Code Using Git Bisect?

As we know every contribution to the projects is stored in the Git log history. So, you can simply start the bisect mode in Git and specify the particular commits in which the code/feature was working. Then, tell Git to look for a bad version from that particular commit. Doing this will let you know about the particular commit where the problem has occurred. Just follow us in the below-mentioned steps to check this process.

Step 1: Go to Repository

Open Git Bash and go to the project repository using the “cd” command:

cd "C:\Users\Git\bisect"

 

Step 2: Check the Project File

In our project directory “bisect”, there is a “file.txt” file having the 6 code commit lines as shown:

cat code.txt

 

Step 3: Check the Log Status

To check the commit log history of the “code.txt” file, execute this command:

git log --oneline

 

There are 6 commits in the file and currently, our HEAD is pointing to commit 6.

Step 4: Start Git Bisect

To debug the code, activate the bisect mode with the following command:

git bisect start

 

The bisecting mode has been turned on.

Step 5: Define Good Code

Suppose your code feature is not working at the current commit but you last tested the feature on the “commit 3” in which it was working. So, simply copy the SHA of commit 3 and specify it in the provided command as good:

git bisect good 2c39869

 

The output shows that Git is waiting for the bad commit.

Step 6: Debug Code

Now, execute the following command to debug the code from commit 3 onward:

git bisect bad

 

As you can see the issue appeared in commit 5.

Step 7: Check File

If we check the output of the file, it will be shifted to the commit 5 as shown below:

cat code.txt

 

Step 8: Debug Code Again

Let’s assume that you are still getting the problem in the code and want to debug the code again. To do so, run this command:

git bisect bad

 

Now, our head is shifted to commit 4. It means that the problem appeared in commit 4.

Step 9: Check File Again

Check the code file output using the “cat” command:

cat code.txt

 

Our file content is replaced with commit 4.

How to Reset and Return From Bisecting Mode?

To reset and return from the bisecting mode, you can simply execute the “git bisect reset” command. Let’s have a quick look at the following in 2-steps.

Step 1: Reset Bisecting Mode

To reset the bisecting mode, run this command:

git bisect reset

 

Step 2: Check File

Check the output of the file through the “cat” command:

cat code.txt

 

The code file has been back in the latest format commit 6.

Conclusion

Git bisect is the tool in Git Bash for efficiently debugging the bug from the code. To do so, open Git bash and go to the project repository. After that, display the Git log history and select the SHA hash of the particular commit that you think the code successfully worked on. Then, declare that commit as good and run the “git bisect bad” command to debug. This tutorial has demonstrated the procedure to debug the code in the project.

Share Button

Source: linuxhint.com

Leave a Reply