DEV Community

Cover image for Get more value from your git log [video]
Dom Habersack
Dom Habersack

Posted on • Originally published at islovely.co

Get more value from your git log [video]

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

A more compact version of the git log that shows more commits and their branches in the 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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

A list of commits and their branches, with only their hashes and no other information shown.

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]'
Enter fullscreen mode Exit fullscreen mode

The same log as before, now with date, time, and author of the commit.

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'
Enter fullscreen mode Exit fullscreen mode

The same log as before, now with the commit subjects for all commits on a second line.

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'
Enter fullscreen mode Exit fullscreen mode

The log from before, now colored.

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'"
Enter fullscreen mode Exit fullscreen mode

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

$ git mylog
Enter fullscreen mode Exit fullscreen mode

The final, colored output of our custom git alias.

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

Top comments (0)