hero

Table of contents

Git bisect is here to save your day

This blog discusses how we can use git bisect command to find commit that has introduced bug recently.

What is git bisect ?

Git command that uses binary search to find the commit that introduced a bug.

When to use git bisect ?

  1. You can use git bisect to find out which commit caused the bug
  2. You might be looking for the commit that introduced a particular fix/feature, I this caseyou can use the terms "old" and "new", respectively, in place of "good" and "bad". git bisect will report which commit introduced the feature/fix

How to use git bisect ?

I will take very easy example of a button that is supposed to navigate user to /blog path. But in recent commits this is broken. Lets find out the commit which broke it. Here is the recent commit history

image alt text

  1. You need to tell git you want to start bisect, then you need to provide which is the bad commit (recent one most times) and which was the good commit.
$ git bisect start
$ git bisect bad
/* I have not provided any bad commit,
   So git will consider latest as bad commit
*/
$ git bisect good db32414
  1. Git bisect will pick a commit between those two endpoints and ask you whether the selected commit is "good" or "bad". Test if the bug is present or not and update same with git bisect bad or git bisect good command.

  2. Keep following step 2. Git bisect will continue narrowing down the range until it finds the exact commit that introduced the change.

image alt text

  1. Now you can check files changed in this commit and easily find out what caused it.
$ git show commitID

I can see this commit has changed the to path to incorrect value.

image alt text

  1. When you are done you need to tell git to stop the bisect process
$ git bisect reset

Gotchas

  1. There can be a case in git bisect when your commit is faling build process and you are not able to test if commit is bad or good, But you know this commit is nothing to do with the bug, You can skip this commit and move to next one.
$ git bisect skip

Conclusion

If you use git bisect, you can save a lot of time (that you will spend into debugging). and narrow down the code you need to check to resolve bug.