skip to content
Alvin Lucillo

Using git bisect to look for the first bad commit

/ 3 min read

Git bisect can help you identify which commit introduced the bug in the app. This works by first setting the last known good and bad commits. Good commit is the commit where the app is working, and bad commit where the app is not working. If you have multiple candidate commits, just pick one for good and bad commits candidate that pass and fail using with your tests, respectively. Git bisect does this by performing a binary search algorithm to find a commit and set it as the HEAD, meaning that commit will be the latest commit. Then you can either pass a script/executable to Git or you can do the manual test.

In this demo, I will let Git perform the test with npm test. In its current state, the project fails. The issue happened after a recent Angular upgrade. npm test has a custom script that performs compatibility check.

npm test

> auth0-angular-bisect-demo@0.1.0 test
> node scripts/check-auth0-angular-compat.js

Incompatible versions: Angular 21.0.0 needs @auth0/auth0-angular 3.x or newer. Current Auth0 SDK is 2.2.3.

Let’s check the Git log. In this scenario, we can set d128dce as the last known good commit where the app was surely working, and 5220a3b is the latest commit, and we’re sure the app is not working with the latest npm test. The bug was introduced somewhere in between the logs.

git log --oneline

5220a3b (HEAD -> main) 10 document bisect goal
8513476 09 add production environment
d9ecdfa 08 add route placeholder
051ab2b 07 update home page copy
2ea59b6 06 upgrade Angular without Auth0 SDK
904820a 05 document project timeline
c1b8147 04 add environment config
b873da7 03 add home component
7adc7c4 02 add Auth0 Angular SDK
d128dce 01 create Angular 16 project

Let’s start git bisect.

git bisect start # starts the session
status: waiting for both good and bad commits

git bisect good d128dce # sets the good commit
status: waiting for bad commit, 1 good commit known

git bisect bad HEAD # sets the bad commit
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[904820a7ecd41b3201836584c7ae7e5cab6a0bb8] 05 document project timeline

git bisect run npm test # the script that bisect will run
running 'npm' 'test'

> auth0-angular-bisect-demo@0.1.0 test
> node scripts/check-auth0-angular-compat.js

Compatible versions: Angular 16.2.12, @auth0/auth0-angular 2.2.3
Bisecting: 2 revisions left to test after this (roughly 1 step)
[051ab2bd4865dc24a136ddb401395d40b038c77b] 07 update home page copy
running 'npm' 'test'

> auth0-angular-bisect-demo@0.1.0 test
> node scripts/check-auth0-angular-compat.js

Incompatible versions: Angular 21.0.0 needs @auth0/auth0-angular 3.x or newer. Current Auth0 SDK is 2.2.3.
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[2ea59b614121aa89bcdf3d2e611cecc1dbdc3fe3] 06 upgrade Angular without Auth0 SDK
running 'npm' 'test'

> auth0-angular-bisect-demo@0.1.0 test
> node scripts/check-auth0-angular-compat.js

Incompatible versions: Angular 21.0.0 needs @auth0/auth0-angular 3.x or newer. Current Auth0 SDK is 2.2.3.
2ea59b614121aa89bcdf3d2e611cecc1dbdc3fe3 is the first bad commit
commit 2ea59b614121aa89bcdf3d2e611cecc1dbdc3fe3
Author: [redacted]
Date:   [redacted]

    06 upgrade Angular without Auth0 SDK

 package.json | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
bisect found first bad commit

We can see the pattern; it tests 05, 07, then finally 06, which is the first bad commit (2ea59b6). That is binary search working. Git starts from the middle 05, and when the script didn’t detect any issues, that means the bad commit happened after 05, so it picked the middle commit between 06 and 10, which is roughly 07. 07 did have the issue, but since Git bisect will find the first bad commit, the only next commit to test is 06.

Be sure to run git bisect reset to reset the bisect session.