Git Bisect

What is Git Bisect and how to use it? - I should have Blue background btw!

What is Git Bisect?

Git bisect allows you to use binary search on your project commits to find a specific commit where a problem started!

Sometimes it can be difficult to find the root of a bug when looking at the whole project, however if we can find the commit where the bug was introduced it usually can give you a better idea of what is wrong, and limits the amount of code you need to investigate to get to the issue.

So if you have an undesired behavior or a bug now, but you know that this was correct in the past, you can use git bisect to help you find the exact commit the issue began, which often makes it easier to find and fix the problem

As i mentioned, Git Bisect uses a binary search algorithm to find the problem commit, after you indicate a bad and a good commit, it will checkout a commit exactly in the middle of those two and then ask you to flag this checked out commit as either bad or good, depending if the bug still happens at this point or not.

It will then pass to the next step, which is finding a new commit between the most recent commit flagged as good and the most recent flagged as bad and it will repeat this steps until we get to the specific commit that introduced the bug.

How to use Git Bisect?

Before we start, we're gonna be checking out a lot of commits, so it's best to commit or stash any changes before we begin!

To start the git bisect process use the command git-bisect start

For it to really start we need to provide two more pieces of information to git, a bad commit and a good commit.

To do that we simply do git bisect bad BAD-COMMIT-IDENTIFIER and then git bisect good GOOD-COMMIT-IDENTIFIER

A commit identifier is, as the name implies, something to identify the commit you're referencing. Usually this is the commit hash, but it could also be a tag, or if you want to flag the current commit as either good or bad you can omit the identifier

Once you started git bisect and gave it a good and a bad commit, git will start working for you.

It'll then check us out to a commit between the ones we flagged as good and bad. We then should check this commit for the behavior we're looking for and mark this one too as good or bad with the corresponding command, either git bisect good or git bisect bad accordingly.

Any questions?

If you need more info latter you can also check the Git Bisect Documentation!