I am now on YouTube!

My channel

Get more value from your git log

Dom Habersack

November 25, 2020

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.

$ git log
The default output of a git log.

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.

$ git mylog
This shows us much more information in the exact same space.

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.

$ git log --graph --date=local
The default log with lines for branches and without the timezone qualifiers.

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).

$ git log --graph --local \
This is cleaner, but maybe too much so.

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.

$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]'
This gives us some more context.

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.

$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]%n        %s'
This finally shows us helpful information, but it’s not very easy to parse yet.

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.

$ git log --graph --date=local \
--pretty=format:'%Cred%h%d %Cgreen%ad %Cblue[%an]%n        %s'
Color makes it easier to see differences between the types of information.

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 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:

$ 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:

$ git mylog
We don’t have to memorize the full command if we put it behind an alias.

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

– Dom

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.