Get more value from your git log

Posted on

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
$ git log
The default output of a 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
$ git mylog
A more compact version of the git log that shows more commits and their branches in the same space.
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
$ git log --graph --date=local
The default log with lines for branches and without the timezone qualifiers.
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 \
--pretty=format:'%h%d'
$ git log --graph --local \
--pretty=format:'%h%d'
A list of commits and their branches, with only their hashes and no other information shown.
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]'
$ 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.
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'
$ 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.
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'
$ git log --graph --date=local \
--pretty=format:'%Cred%h%d %Cgreen%ad %Cblue[%an]%n        %s'
The log from before, now colored.
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'"
$ 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
$ git mylog
The final, colored output of our custom git alias.
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!

Debug
none
Grid overlay