Find the commit that introduced a bug in your code: how to use git bisect in 7 steps

flaviabastos profile image Flávia Bastos Originally published at flaviabastos.ca on ・3 min read

When I first heard about git bisect I thought it sounded scary and complicated, so I never looked for an opportunity to learn more about it and use it. That’s until last week when I ran into a bug in our master branch. I knew that the bug was not there two days before so I tested an earlier commit and confirmed that that older commit was a good one. Now, we had tens of commits in between. How to find out when exactly the bug was introduced? It would be impractical to check and test each individual commit.

Enter git bisect for the rescue! 🦸‍♀️

When to use git bisect

When you want to find out which commit introduced a bug in your code.

How to use git bisect

step 1. Find two commits to use as comparison: one with the bug and one without the bug.

step 2. Get those commits hashes: run git log and copy the hash for these two commits:

git log

If your last commit is the bad one, you don’t need to find its hash. You can use ‘HEAD‘ – see below.

step 3. Enter in bisect mode: run git bisect start

step 4. Tell git which one is your bad commit: git bisect bad <commit hash> or HEAD

git bisect bad HEAD

step 5. Now tell git the good commit hash: git bisect good <commit hash>

git bisect good 78cdc0fbe34aadb2ca5148d3877257664cfb69a1

step 6. At this point git will put you in a different commit in your code (some commit in between those two) so you can test and tell git what was the result. If this commit is good, run git bisect good or run git bisect bad if that commit has the bug.

The more commits you have in between the two points in time, the more steps you will have to run through but it’s as simple as that: just test and give a feedback.

At the end, git will tell you which commit introduced the bug:

0088810e9ce4a6abef5923a3674be71599112abc is the first bad commitcommit 0088810e9ce4a6abef5923a3674be71599112abcAuthor: FlaviaBastos <my\_email\_address\_here@email.com>Date: Fri Mar 20 19:21:18 2020 -0400 Adjust mobile styling:100644 100644 72adc1919a30e436d6bbeb387bcaf52ea329bd35 10a77070785f4f7d2a7732c8b6a6dd48616e4643 Mindex.html:100644 100644 30071a4c36b1e8df0caaf3633b37bdf116de3837 fb895eab1f531a859635ddc877ffb3944b4f5245 Mstyle.css

It was mind blowing! 🤯

step 7. Now you can exit bisect mode by running git bisect reset and that will put you back in the same commit where you started you research.

Show me! 🕵

flavia:~/code/my\_project$ git log
commit 0088810e9ce4a6abef5923a3674be71599112abc (HEAD -> master, origin/master)
Author: FlaviaBastos <my\_email\_address\_here@email.com>
Date: Fri Mar 20 19:21:18 2020 -0400 

Adjust mobile styling

Author: FlaviaBastos <my\_email\_address\_here@email.com>
Date: Fri Mar 20 17:41:52 2020 -0400 

Add reset basic styling - not sure about this

commit 78cdc0fbe34aadb2ca5148d3877257664cfb69a1
Author: FlaviaBastos <my\_email\_address\_here@email.com>
Date: Fri Mar 20 17:29:00 2020 -0400 

Add topics section

commit b34e2a36f29cbf03369b444013351e24b6a39d51
Author: FlaviaBastos <my\_email\_address\_here@email.com>
Date: Fri Mar 20 17:18:19 2020 -0400 

Add 'who is this for'

flavia:~/code/my\_project$ git bisect start

flavia:~/code/my\_project$ git bisect bad HEAD

flavia:~/code/my\_project$ git bisect good b34e2a36f29cbf03369b444013351e24b6a39d51

Bisecting: 0 revisions left to test after this (roughly 1 step)[b584bf9491aa971216ad87506e6ea765537d0214] Add reset basic styling - not sure about this

flavia:~/code/my\_project$ git bisect good0088810e9ce4a6abef5923a3674be71599112abc is the first bad commit

commit 0088810e9ce4a6abef5923a3674be71599112abcA
uthor: FlaviaBastos <my\_email\_address\_here@email.com>
Date: Fri Mar 20 19:21:18 2020 -0400 

Adjust mobile styling

:100644 100644 72adc1919a30e436d6bbeb387bcaf52ea329bd35 10a77070785f4f7d2a7732c8b6a6dd48616e4643 Mindex.html
:100644 100644 30071a4c36b1e8df0caaf3633b37bdf116de3837 fb895eab1f531a859635ddc877ffb3944b4f5245 Mstyle.css

flavia:~/code/my\_project$ git bisect reset

Previous HEAD position was b584bf9 Add reset basic styling - not sure about this
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Also note: git bisect doesn’t change any code or commits. Like git status and git log, it’s purely an investigation tool. Go ahead and try it out!

Happy bug chasing!

If you found this helpful, let me know on Twitter!

The post _Find the commit that introduced a bug in your code: how to use git bisect in 7 steps was originally published at _flaviabastos.ca

Photo by Agence Olloweb on Unsplash

Posted on by:


markdown guide