Working out when something broke using Git Bisect and Git Log
Tags: git
Published: 2020-01-22 08:22:17

This post was originally posted <a href="https://www.pmichaels.net/2019/03/22/working-out-when-something-broke-using-git-bisect-and-git-log/">here</a> and is by far my favourite git feature.

<h1>Something has broken</h1>

You're giving the app a quick test, and suddenly you realise that something in the application has stopped working (or maybe it's started, but it shouldn't have).  You remember that two days ago, this particular thing was working fine.  This post is going to cover <em>manually</em> using git bisect to determine when your code was broken.

<h1>Get the good commit</h1>

To use git bisect, you need a good commit and a bad commit.  Obviously, git doesn't know what's good or bad, so you could use it to determine when something was changed, added, or removed.  Providing the behaviour is different, you can identify it.

If you know the date of the 'good' commit, you can ask git for the logs after that date.  To list commits after a given date:

```
git log --after="2019-03-19T00:00:00-00:00"
```

This will give you all the commits (you can add a '--before', too if you're dealing with a long time ago.  When you get the git Commit Id, make a note of it; for example:
       
```
Bc1afa359dc98b899cc4194ec8130a6229643172
```

<h1>Let's do some bisecting</h1> 

You start the bisect process by:

```
git bisect start
```

At all times through this process, git does precisely nothing to help you along, so if you even get an acknowledgement of your command you should consider yourself lucky!

The next step is to tell git that the current release is bad:

```
git bisect bad
```

You can give it a commit here that isn't the current release, if you so choose.

The final step to start the process is to tell it where to start - i.e. what was the last known good release.  This is your commit Id from earlier:

```
git bisect good Bc1afa359dc98b899cc4194ec8130a6229643172
```

What git does here is a binary chop.  So the first code it will check out will be halfway between the two commits.  It then tells you nothing (as usual).  Now that it's checked out the code for you, run your application with the code and see whether it is 'good' or 'bad'.  Once you've determined that, go back to git and tell it:

```
git bisect good
```

Or:

```
git bisect bad
```

Each time you tell it it's good or bad, it will check out new code, and you need to test again:

<a href="https://www.pmichaels.net/wp-content/uploads/2019/03/git-bisect.png"><img src="https://www.pmichaels.net/wp-content/uploads/2019/03/git-bisect.png" alt="" width="913" height="374" class="alignnone size-full wp-image-3675" /></a>

(apologies for all the redacting)

Finally, it will determine which commit was the first bad one, and report back:

```
$ git bisect bad
Eea8e3d72fcb26cdebb2dbf2c13fdd88d7f3782a is the first bad commit
commit Eea8e3d72fcb26cdebb2dbf2c13fdd88d7f3782a
Author: Ian Kilmister <ian.kilmister@motorhead.com>
Date:   Wed Mar 20 11:49:18 2019 +0100

    New lyrics

```

Once you're done, you need to reset - this will book out the code you had out before you started:

```
git bisect reset
```