Keep calm and git bisect
Tags: git, programming, bisect, binary
Published: 2020-12-20 14:17:55

---
title: Keep calm and git bisect
published: true
description: Your app broke, and you have no idea why? It is important to keep calm and track that first commit that is causing the failure, and git can help you out. Let's see how...
tags: git, programming, bisect, binary
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/78x2tqcmodv4t9u79m9d.png
cannonical_url: https://konrad-126.hashnode.dev/keep-calm-and-git-bisect
---

Your app broke, and you have no idea why? Even worse, you don't even know what commit is causing it to fail? Don't worry - it happens.

It is important to keep calm and track that first commit that is causing the failure, and git can help you out. Let's see how...

# Finding the (first) bad apple

You start with what you know for sure - the last (present) commit is bad:
![sequantial_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471667627/U6a68V7L8.png)
Then you checkout to some (potentially distant) commit in the past where you know things worked (you check it). Let's call it the good commit:

![sequantial_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471679100/q4xpBxBCU.png)

You can be sure that the first bad commit is somewhere between that good commit and the present bad commit:

![sequantial_3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471696058/MBxvT0UoR.png)

To trace it, you can sequentially go through all those in-between commits and check your app at every step.

This process will work fine if there are a few commits to check, but what if there are tens or hundreds?

Because git's commit-graph, in this context, is a sorted list, we can speed up the process by using an algorithm called  [binary search](https://www.geeksforgeeks.org/binary-search/) .

# Binary search to the rescue!

You start the same way as before - finding some commit in the (distant) past where things were nice and green:

![binary_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471707788/sQZZXmBvm.png)

Now, instead of sequentially checking all the in-between commits, you pick the commit in the middle of your list and check your app's behavior at that point:

![binary_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471746763/4VrNxlqpR.png)

If everything works you can consider that commit as a good commit:

![binary_good_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471762079/O6ldynj2x.png)

You can also be sure that all the commits before it are also good so you don't need to check them individually:

![binary_good_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471773217/cCTXIAIAa.png)

If, on the other hand, the commit in the middle was bad (your app was broken):

![binary_bad_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471785960/u1Dt5m2lM.png)

Then you'd know for sure that all the commits after it are also bad:

![binary_bad_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471797107/kMmZtu1Es.png)
Either way, you end up with a new list of commits to check that's half in size (with a new god/bad commit pair).

![binary_second_Step.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471808556/BnVlulDpd.png)

You repeat this process until you pinpoint the exact commit where things went sour. 

Using binary search significantly cuts the time needed to trace the first bad commit, but all this manual checking out to commits still takes time and it's not much fun. Luckily, git has a command called ** [bisect](https://git-scm.com/docs/git-bisect) ** that will do that for you. All you'll need to do is to decide whether a certain commit is good or bad and git will do the rest.

# Bisect

To start the bisecting process you type:

![cali_start.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471824178/1MZtJhlz-.png)

Then you tell git what commit is bad. If you omit the checksum git will assume it's the last commit on your current branch.

![cli_bisect_bad.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471859191/_eqN1Hg-r.png)

After you tell git what is the "good" commit:

![cli_bisect_good.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471871056/IEqnWUKTv.png)

it will display how many steps are left and immediately checkout to that first (middle) commit. Now you can check your app's behavior and tell git if the commit is good or bad:

![cli_bisect_step.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471897082/v7N9FpvRf.png)

Once you do, git moves (checks-out) to the next commit and you repeat the process. After a couple of steps (depending on how big your commit list was), you'll reach the final step and git will inform you what the first bad commit is:

![cli_bisect_final_step.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471910103/mJmXnLx0D.png)

# Full Automation

Manually checking those commits to decide if they are good or bad is sometimes necessary, maybe it's some legacy app and there's not a lot of tests so you need to manually check your app. If you are lucky and can write a script to check it for you then becomes fully automated.

The script needs to be outside of source control (maybe a folder "up") and return exit code 0 if the commit is considered good or 1 if it's considered bad.

You start the bisect as before, by telling git which commit is good and which is bad. 

![cli_bisect_run_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608471938666/6s5HwZvD_.png)

But now you can tell git to run the bisect process and use your script to decide whether some commit is good or bad:

![cli_bisect_run_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608472849818/wKSBlP3HR.png)

------

Now you know what commit was the one to introduce the failure (bug), hopefully, you'll be able to figure out the why part of the question.