Get more value from your git log
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
$ git log
$ 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
$ git mylog
$ git mylog
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
$ git log --graph --date=local
$ git log --graph --date=local
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'
$ git log --graph --local \
--pretty=format:'%h%d'
$ git log --graph --local \
--pretty=format:'%h%d'
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]'
$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]'
$ git log --graph --date=local \
--pretty=format:'%h%d %ad [%an]'
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'
$ 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'
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'
$ 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'
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'"
$ 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
$ git mylog
$ git mylog
If you customized your git log, I’d love to see a screenshot!