DEV Community

Adel Emad
Adel Emad

Posted on

Git Bisect, When and How?

In this article, we will learn about the git bisect command which is rarely used but can be helpful in some cases.

When can we use git bisect?

according to git docs, git bisect is used to find a commit that produces a bug ; however, I do say that it can be used to find a commit that produces anything you can notice in the program output; for example:

  • Log statement (ex. console.log)
  • Enhancement to a certain part ..etc

Therefore, if you are asking where I can find the commit that is responsible for certain output, you can use git bisect command

When not to use it?

If you want to know the commit or person that is responsible for a certain part of the code, you can use Gitlens

Use git bisect Do not use git bisect
You want To get the commit and its info To get the commit and its info
You have Output not code.. for example:
- Bug you can reproduce
- log statement
Code so you can use another tool to get the commit info

How can I use git bisect ๐Ÿค”?

First, detect the output that you are searching for its commit info, then run

git bisect start  
Enter fullscreen mode Exit fullscreen mode

then run

git bisect good [hash of commit]
Enter fullscreen mode Exit fullscreen mode

and insert the hash of the commit where you are sure that that certain output is not present.
Then run

git bisect bad [hash of commit]
Enter fullscreen mode Exit fullscreen mode

where you are sure you can reproduce this certain output. you can leave it empty to choose the certain commit.

then git will start jumping between commits that are between the good commit and the bad commit
for every commit, try to reproduce the output that you target, if you can reproduce it, then run git bisect bad;
if you can not reproduce it, run git bisect good

Finally, git will log the commit info that is responsible for the output that you are looking for ๐ŸŽ‰.
Then run git bisect reset to end the process.

I hope this article has helped you understand git bisect command

Top comments (0)