Open In App

Git bisect

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When you notice a bug in your code, and you know that it used to work at some point, you can use git bisect to determine which commit introduced the bug. Instead of checking each commit one by one (which could be time-consuming), git bisect helps you find the culprit by allowing you to mark commits as "good" or "bad."

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.

Implementation of Git bisect command

Let’s say you have a repository where you found a bug in the code. Here’s how you would implement git bisect step by step:

Step 1: Start the bisect process

git bisect start
file

Step 2: Mark the current commit as bad

Assume HEAD is the bad commit

git bisect bad
file

Step 3: Identify a known good commit

Replace <good_commit_hash> with the actual hash of the last known good commit

git bisect good <good_commit_hash>
file
file

Step 4: Test each commit

  • Git will now check out a commit in the middle of the range. Run your tests:
  • Run your test command
./run-tests.sh  
  • If the tests fail (bug is present):
git bisect bad
  • If the tests pass (bug is not present):
git bisect good

Step 5: Repeat until you find the offending commit

Git will continue to provide you with commits to test. Follow the previous step until you see a message indicating which commit introduced the bug.

Output:

file

Conclusion

git bisect is a highly efficient way to track down bugs in your Git history. By systematically narrowing down the range of commits, you can quickly find the introduction point of an issue, saving you time and effort. Happy coding!


Next Article
Article Tags :

Similar Reads