git bisect command

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.

The git bisect helps us 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 command

Basic bisect commands Let us look at the various commands associated with the bisect command. • git bisect start: This command is used to make the Git work in the bisect mode apart from this, it also lets the Git wait for the further bisect commands. • git bisect bad (commit hash or tag): This command is used to inform the commit that is bad. It can provide the hash or tag that we want.

Bash

git bisect bad

If we do not specify the commit hash then by default the current commit is marked as bad commit. • git bisect good (commit hash or tag): This command is used to inform the commit that is good. It can also provide the hash or tag that we want.

Bash

git bisect good v2.0

Here, v2.0 is the tag name. • git bisect reset: As the name suggests, this command is used to exit the bisect mode, and returns to the original HEAD. By default, it will return to the commit, when the git bisect start command is executed.
But we have an option to switch to the particular commit also by following the command

Bash

git bisect reset <commit-id>

Demonstration of git bisect Process

The following steps demonstrates the use of git bisect to find the faulty commit.
Step 1: First type the command git bisect start.
Step 2: Then specify the bad and good commit by using the following commands: Syntax for specifying bad commit git bisect bad commit-id Syntax for specifying good commit git bisect good commit-id

Step 3: After specifying the good and bad commit the git starts the revision process, from bad commit to the good commit, we can specify the current revision commit is bad or good by using the command git bisect bad and git bisect good simultaneously.
We are then given a series of commits where we check the contents of the file each time to see if the commit is good or not (based on some spelling error). Eventually, we narrow down to the commit to where spelling in the code is changed.
The below is the series of commits which we get using the git log command.

Bash

$ git log
commit d30da61d4cd8a68144332e2f6c673a1c2986d1fa (HEAD -> main)
Author: Ashish <example@example.com>
Date:   Wed Jun 12 11:08:47 2024 +0530

    third paragraph is added

commit bd3cc644d642bfb91fe825d5fbb18eebcf361bd2
Author: Ashish <example@example.com>
Date:   Wed Jun 12 11:07:40 2024 +0530

    second paragraph is added and spelling mistake in first paragraph

commit 4057a4299bd1f1aeed7d0a6e7f01602c34278086
Author: Ashish <example@example.com>
Date:   Wed Jun 12 11:06:35 2024 +0530

    first paragraph is added

commit 14b310e02e7789cf580c12ea193a1044176cc66f
Author: Ashish <example@example.com>
Date:   Wed Jun 12 11:04:57 2024 +0530

    heading is added

commit 3de9512e90a16ce24f291e04b0759684d38526d2
Author: Ashish <example@example.com>
Date:   Wed Jun 12 11:04:09 2024 +0530

    Boilerplate code added  

Let’s start the git bisect process by using the following command:

Bash

git bisect start
git bisect command

We can now categorize the any commit as good, here we are specifying the second commit as good, and the last commit as bad.

Bash

git bisect good 14b310
git bisect command

Bash

git bisect bad d30da61
git bisect command

Note the revision start from the bad commit towards the good commit. It shows us the git commit id and the git commit also on which it is currently. At this point, we can check our code and can specify that this commit is considered as good or bad, by typing git bisect good or git bisect bad.

git bisect command

Now when I have seen in the code, I have the correct code, then I will specify this commit as good by typing git commit good.

git bisect command

Now in the above image we can see that which commit is first bad commit.
To exit the git bisect process, we can execute the command git bisect reset.

git bisect command