What is Git Bisect?

The git bisect command is used to discover the commit that has introduced a bug in the code. It helps track down the commit where the code works and the commit where it does not, hence, tracking down the commit that introduced the bug into the code.

Suppose there are many commits, and we have to find the commit that introduced the bug. The normal way to do this would be to check out each commit one by one and build it until we reach the commit where the code does not compile. This is essentially a linear search and may take a long time depending on the number of commits. git bisect helps to find the faulty commit by performing a binary search on the commits to reduce the time taken to find the faulty commit.

git bisect performs a binary search to find the faulty commit.

Git bisect requires the user to input the “good” commit and the “bad” commit, and then performs a binary search on the commits, starting with the commit that is at the midpoint between the “good” and “bad” commit.

Code

The following code demonstrates the use of git bisect to find the faulty commit.

1. Initializing the repository

mkdir git_bisect_example
cd git_bisect_example
git init
Terminal 1
Terminal
Loading...

2. Creating commits to demonstrate git bisect

Terminal 1
Terminal
Loading...

3. Finding the log history and narrowing down the bad commit.

The code below shows all the commits made in the folder. We then start git bisect and categorize the first commit as a good commit and the last commit as a bad commit. We are then given a series of commits where we check the contents of the text file each time to see if the commit is good or not (based on the presence of 2). Eventually, we narrow down to the commit to where 2 gets changed to 9.

Terminal 1
Terminal
Loading...
commit 9598ae96a6bba2cbd4eda693f8aa6cd92fd4c295
Author: Example <example@example.com>
Date:   Fri Apr 9 11:01:57 2021 +0000

    Adding 4

commit b75591336fe4f603fd36eaf6354da8a652025186
Author: Example <example@example.com>
Date:   Fri Apr 9 11:01:57 2021 +0000

    Changing the 2 to 9

commit a59e5dd4632361015856367535d95cbb493a07d3
Author: Example <example@example.com>
Date:   Fri Apr 9 11:01:57 2021 +0000

    Adding 3

commit 67495ba9c2dec29abc00af776e6cd042fe09a477
Author: Example <example@example.com>
Date:   Fri Apr 9 11:01:57 2021 +0000

    Adding 2

commit 548c695b9d5f6906e30507de8d672b82f088f5a3
Author: Example <example@example.com>
Date:   Fri Apr 9 11:01:57 2021 +0000

    Adding 1

We can now categorize the first commit as good and the last commit as bad. We check the contents of the test file and, since the number 2 is still present, we categorize the commit as good.

The commit is categorized as "good" since 3 is present in the text file.
The commit is categorized as "good" since 3 is present in the text file.

git bisect now takes us to another commit. We repeat the process and find the new commit to be bad since it contains the number 9 instead of the number 2.

The file contains the 9 instead of 2, hence the commit is bad.
The file contains the 9 instead of 2, hence the commit is bad.

git bisect finally returns the commit to us where the first error took place.

Git bisect return to us the first bad commit.
Git bisect return to us the first bad commit.