DEV Community

Cover image for Git Bisect is Easy (How to Initiate the Robot Uprising)

Git Bisect is Easy (How to Initiate the Robot Uprising)

Jacob Herrington (he/him) on August 08, 2019

When we find a new bug in our applications or a test randomly fails in CI, frequently, the bug came to exist as the unintended side effect of anoth...
Collapse
 
matthewpersico profile image
Matthew O. Persico

But what if I am not trying to find the transition between a good and a bad state. What if I am trying to find some other change, like when we added some particular function?

Well, the "test" to execute would be 'git grep theFunctionName'. But the 'good' followed by 'bad' is exactly the opposite logic to use.

You can change the names of what you use to indicate the transition. In this case:

git bisect start --term-old=dne --term-new=exists
git bisect exists
git bisect dne someOldhash
git grep -q theFunctionName && git bisect exists || git bisect dne

Repeat the last command until you get output.

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Cool! Yeah this is a contrived example, but for people unfamiliar it's useful. The cool thing about git is that it can be used really creatively to solve problems like this really quickly.

Collapse
 
beatngu1101 profile image
Daniel Kraus

Thanks Jacob, really cool blog post! 👍 git bisect is great, but it's even better when combined with micro commits. This really helps to pin down faulty code.

Collapse
 
grumpytechdude profile image
Alex Sinclair

Thanks for explaining it so well! I really struggle to understand when I'd use this in my workflow, and you did a great job showing examples of that.

So, if all the tests ran on every commit, this would be less useful- unless they were something like performance tests that were relatively subjective.

Usually I try and find the bug, then look at the history for that line or file to see why that change was made, in effect doing almost the opposite of bisect.

I'll try and use bisect, see if it improves my!

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Yes, the real beauty of bisect is that it does a binary search, which makes it way faster and easier to find the broken commit

Collapse
 
joeattardi profile image
Joe Attardi • Edited

Git bisect is amazing. It has saved me several times!

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

I think it's great, but I've found a lot of developers are afraid of it because it seems complicated.

Collapse
 
joeattardi profile image
Joe Attardi

It definitely intimidated me when I was first getting familiar with Git. But once I was trying to find a big one commit at a time, and decided to give it a try. Now it’s my go to tool for finding a bad commit!

Collapse
 
bence42 profile image
Bence Szabo

Nice summary! Gives insight beside listing the necessary commands. Would be nice to have a part 2 that shows the automation of the whole thing by passing a shell script to bisect.

Collapse
 
areahints profile image
Areahints

Very easy to grasp! Thanks

Collapse
 
imjacobchen profile image
Jacob Chen

Great tutorial - very easy to follow. Thanks!

Collapse
 
nanohard profile image
William Antonelli

Thanks so much for this!

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

No problem, glad someone finds it useful. 🤠