Using `git bisect` to find the faulty commit

Introduction

Yesterday I was working on a task and I made changes in a few files. When committing the changes, right before pushing them to a remote branch, I added a couple of more small changes, created a few more commits and done, pushed …


This content originally appeared on DEV Community and was authored by Jessica Alves

Introduction

Yesterday I was working on a task and I made changes in a few files. When committing the changes, right before pushing them to a remote branch, I added a couple of more small changes, created a few more commits and done, pushed all the commits. When running the application again I got an error, which I couldn't identify which of the changes or commits was related to.
And that's when git bisect came across while I was trying to debug.

What does the git bisect command do?

git bisect is a command in Git that helps find the commit that introduced a bug or caused a regression in your codebase. It uses a binary search algorithm to efficiently narrow down the range of commits to be examined. By marking specific commits as “good” or “bad”, git bisect automatically selects the next commit for testing until it identifies the exact commit that introduced the issue. This process helps identify the faulty commit more quickly and efficiently, enabling developers to pinpoint and fix the problem more effectively.

An example on how to use it

Using the GitLens extension in VSCode I picked one of the previous commits guessing it didn’t contain any errors (you can use git log to do that as well), and to make sure it was working fine I checked out to that commit and ran the application from that point, checking if the error was there or not. Once it hasn’t any errors I decided to pick that commit to start to bisect.

So I started the git bisect with the flawed commit id - which was my last commit - to indicate it’s a bad commit:

git bisect start <flawed_commit_id>

Git will mark it as a bad commit and then expect for the id of a good commit. So I copied and pasted the id of that commit I ran previously to make sure it wasn’t broken in order to run the following command:

git bisect good <good_commit_id>

Git will now automatically checkout a commit in the middle of the range between the known good commit and the bad commit. This commit represents the midpoint of the binary search. So I was able to run my application using the returned commit to test if the application was already crashed or not, so that I could tell Git to mark the commit as good or bad using git bisect good or git bisect bad.

Repeat the process: Git will continue selecting commits for testing based on the binary search algorithm. You repeat the previous step by testing the code and telling Git to mark it as good or bad commit until you narrow down to the exact commit that introduced the bug.

Finish the bisect: Once you identify the faulty commit, you can run git bisect reset to exit the bisect mode and return to the latest commit.

Conclusion

By systematically cutting down the range of commits through binary search, git bisect helps efficiently locate the faulty commit in your code history.


This content originally appeared on DEV Community and was authored by Jessica Alves


Print Share Comment Cite Upload Translate Updates
APA

Jessica Alves | Sciencx (2023-05-13T21:46:21+00:00) Using `git bisect` to find the faulty commit. Retrieved from https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/

MLA
" » Using `git bisect` to find the faulty commit." Jessica Alves | Sciencx - Saturday May 13, 2023, https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/
HARVARD
Jessica Alves | Sciencx Saturday May 13, 2023 » Using `git bisect` to find the faulty commit., viewed ,<https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/>
VANCOUVER
Jessica Alves | Sciencx - » Using `git bisect` to find the faulty commit. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/
CHICAGO
" » Using `git bisect` to find the faulty commit." Jessica Alves | Sciencx - Accessed . https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/
IEEE
" » Using `git bisect` to find the faulty commit." Jessica Alves | Sciencx [Online]. Available: https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/. [Accessed: ]
rf:citation
» Using `git bisect` to find the faulty commit | Jessica Alves | Sciencx | https://www.scien.cx/2023/05/13/using-git-bisect-to-find-the-faulty-commit/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.