Get more value from your git log [video]
Tags: git, github, bash
Published: 2020-12-03 08:12:11

{% youtube zqx19b1lOhc %}

As command line cowboys, we often explore a project’s history through its git log. By default, this command shows little information that takes up a lot of space. It’s difficult to see what happened in a project over time like this.

```bash
$ git log
```

![The default output of a git log.](https://dev-to-uploads.s3.amazonaws.com/i/2lrosdhj3vzunl06ifqd.png)

As most things in git, we can customize what a log looks like. Let’s design ourselves a better, prettier git log. Step by step, we’ll build towards this result.

```bash
$ git mylog
```

![A more compact version of the git log that shows more commits and their branches in the same space.](https://dev-to-uploads.s3.amazonaws.com/i/88z5ez9c1apyno1tbmmj.png)

We’ll start by drawing our branches and using local times. The `--graph` option shows us when branches existed and where we merged them. The `--date` option removes the timezone information. When looking at a repository’s history, the exact timezone is rarely important.

```bash
$ git log --graph --date=local
```

![The default log with lines for branches and without the timezone qualifiers.](https://dev-to-uploads.s3.amazonaws.com/i/61ib1l7ymvlfrwae1ckn.png)

There is still a lot of unnecessary information and too many empty lines here. We can customize the look of each commit with a template passed through the `--pretty` option. We can use some placeholders in there that give us different pieces of information.

While the full hash helps us find a specific commit, the first few characters are usually enough. In our first commit template, we’ll use only the commit’s **short hash** (`%h`) and **ref names** like `HEAD` (`%d`).

```bash
$ git log --graph --local \
--pretty=format:'%h%d'
```

![A list of commits and their branches, with only their hashes and no other information shown.](https://dev-to-uploads.s3.amazonaws.com/i/27c9318x7oxwwh2ceka6.png)

We can now see a lot more commits in the same space, but don’t have any idea what happened in them. Let’s add the **authoring date** (`%ad`) of the commit and the **author’s name** (`%an`). We can use extra non-placeholder symbols like brackets to separate those values.

```bash
$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]'
```

![The same log as before, now with date, time, and author of the commit.](https://dev-to-uploads.s3.amazonaws.com/i/y4ofrk36dl6xdo1aps8h.png)

Finally, we need to add the **subject** (`%s`) of the commit. We’ll put it on a **new line** (`%n`) and add some space before it so it aligns with the time and date.

```bash
$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]%n        %s'
```

![The same log as before, now with the commit subjects for all commits on a second line.](https://dev-to-uploads.s3.amazonaws.com/i/4wmsrjxz5ddsdawmrohn.png)

We can use `%Cred`, `%Cgreen`, and `%Cblue` to add some **color**. All text after one of these appears in that color. The coloration stops at the end of the line. We don’t have to do anything for the commit subject on the second line to keep its default color.

```bash
$ git log --graph --date=local \
--pretty=format:'%Cred%h%d %Cgreen%ad %Cblue[%an]%n        %s'
```

![The log from before, now colored.](https://dev-to-uploads.s3.amazonaws.com/i/3rtphad3ed1z70m41gcb.png)

And there you go! There are a bunch of other placeholders you could use. `%ae` is the **author’s email**, `%ar` a **relative time stamp** like “6 days ago”, and `%b` the full **body** of the commit. You can find [all placeholders in the git documentation](https://www.git-scm.com/docs/git-log#_pretty_formats) if you want to further customize your log.

Of course, always typing the full command would be annoying. You can hide your custom log display behind an **alias** like so:

```bash
$ git config --global alias.mylog \
"log --graph --date=local --pretty=format:'%Cred%h%d %Cgreen%ad %Cblue[%an]%n        %s'"
```

By doing that, you can always get to your log by typing `git mylog`:

```bash
$ git mylog
```

![The final, colored output of our custom git alias.](https://dev-to-uploads.s3.amazonaws.com/i/3rtphad3ed1z70m41gcb.png)

If you customized your git log, I’d love to see a screenshot!