Git Tips: Pretty Logs

Git TipsDeveloper Productivity

One of the main goals of this blog will be to productivity tips for developers and aspiring developers. If you're new to software development, one of the rites of passage for become a "true" developers is learning git, and many of my tips will focusing on improving git skills.

Today I'm going to share a tip that will make a huge difference if you are using git on the command line with the default log format. When you run git log, you should see a list of commits that will look something like this:

commit b55e2522e7c1858eff41416e49da333b8d44ab78
Author: Brian Mortenson 
Date:   Wed Jun 17 12:43:32 2020 -0600

    add bacon

commit e4506139a924e93ff706ff2785223041b3c1b507
Author: Brian Mortenson 
Date:   Wed Jun 17 12:43:13 2020 -0600

    simmer on high

commit b4a82a01b19649e50b60925056edc4b17150d8e0
Author: Brian Mortenson 
Date:   Wed Jun 17 12:42:39 2020 -0600

    mince garlic

  ...

Yuck. First of all, each commit occupies six lines of space on the screen. That makes it really hard to scan through and get and idea of what's going on.

Fortunately Git offers a formatting option: --pretty=someformat, or --format=someformat, that allows you to change the way each changeset is printed.

The format string can be take two forms: the name of a built-in format, or a format pattern string. One of the built-in formats is called oneline, and it does what it sounds like. Let's take a look:

b55e252 (HEAD -> master) add bacon
e450613 simmer on high
b4a82a0 mince garlic
13fcb90 preheat oven
f6f54da marinate chicken

Ok, that's better. But there were some useful things in the bigger log, like who authored the commit and when.

I'll show you the log format I use. I've been refinining it since 2011, and think it's the best of both worlds. It looks like this:

b55e252 Brian .. add bacon (HEAD -> master) 84 minutes ago
e450613 Brian .. simmer on high 85 minutes ago
b4a82a0 Brian .. mince garlic 85 minutes ago
13fcb90 Brian .. preheat oven 88 minutes ago
f6f54da Brian .. marinate chicken 2 hours ago

– I like it because it presents a maximal amount of information in a minimal amount of space, while still being easy to read. To use it, add this to your .gitconfig file:

[format]
	pretty = tformat:%C(bold yellow)%h %<(8,trunc)%C(bold blue)%aN%Creset %s%C(bold red)%d %Creset%C(dim white)%ar%Creset%C(white)%+N%Creset

This specifies the default format to use anywhere a changeset is displayed, such as git log or git show. It uses format placeholders such as %s for subject (commit message) and %aN for author name, as well as color placeholders such as %C(bold red).

For more information on how to customize the formats, check out https://git-scm.com/docs/pretty-formats

In the next post, I'll show you another favorite log format, and how to use git aliases to improve your workflow.

Thanks for reading!