I was working on a project and I was making some changes to the code. I was not testing the code as I was making the changes, which was a bad idea (Don’t try this at home). After making some changes, I noticed that something broke.

I had no idea what caused the issue. I had made a lot of changes and I did not know where to start looking.

Linus Torvalds to the Rescue (Again)

I remembered that a friend of mine had told me about something called git bisect. I know what git bisect does, but I had never used it before.

I decided to give it a try.

Using Git Bisect

The way git bisect works is is uses a binary search algorithm to find the commit that introduced a bug.

You simply give git bisect a good commit and a bad commit,

Simply, the good commit is a commit where the code was working as expected, and the bad commit is a commit where the code is broken (the last commit in my case).

Here is how:

  1. Start the bisect process:

    git bisect start
    
  2. Tell git bisect which commit is good and which commit is bad:

    git bisect good <good-commit>
    
    git bisect bad <bad-commit>
    
  3. git bisect will checkout a commit in the middle of the range between the good and bad commits.

  4. Test the code to see if it is working as expected.

    If the code is working as expected, tell git bisect that the commit is good:

    git bisect good
    

    If the code is not working as expected, tell git bisect that the commit is bad:

    git bisect bad
    
  5. git bisect will checkout another commit in the middle of the range between the good and bad commits.

  6. Repeat steps 4 and 5 until git bisect finds the commit that introduced the bug.

  7. When git bisect finds the commit that introduced the bug, it will print the commit hash.

That is it!