The git log
command outputs a lot of useful information—message, author, date, hash, branch—but its default format leaves something to be desired. Mostly, it just takes up too much screen space. Here’s an example, using the popular Python package flask:
The default git log
above has all the information we want, but just two commits already takes up a lot of screen space; what if we wanted to look at the most recent 5 commits, or 10? We’d run out of room and have to scroll. Wouldn’t it be nice to have something much more concise like this?
Git log formatting options
Git log –oneline
The easiest, quickest win for a short git log is the --oneline
option. Here’s an example:
That’s a much more concise display, I could easily take a quick look at the most recent 5, 10, or 20 commits this way without blowing up my screen. If the --oneline
option above is good enough for you, then great! Add it as an alias in your gitconfig and feel free to stop reading here.
However, the default --oneline
output leaves some important information out: the author, the date, and the associated branch.
Were those commits applied directly to main
? Probably not; flask is a hugely popular package and committing directly to main
would be dangerous. So how can we tell?
Git log –graph
The --graph
option addresses the problem of identifying the branches commits were applied to. In git’s own words:
–graph
Draw a text-based graphical representation of the commit history on the left hand side of the output.
Ok, but what does that actually mean? Let’s run the same git log --oneline
but add the --graph
option:
That’s a bit better. The main branch is the vertical one on the left, and sub-branches break off to the right with their associated commits, converging back to the main
branch in a merge commit. This output, while better, is still lacking information on the author and the date. Fortunately, git gives us a ton of options for custom log formatting.
Custom git log formatting
The custom log formats all stem from git log’s --pretty
option. The --oneline
option used above is actually shorthand for git log --pretty=oneline --abrev-commit
, where --abbrev-commit
shortens the commit hash from 40 characters down to 8. For all of the default --pretty
formats, check git’s documentation under pretty formats.
For the rest of this post, I’m going to go over my personal git log format, which I have aliased to git lg
. Here’s the same screenshot from the beginning of the post:
This is the definition of that command in my .gitconfig
file:
[alias]
lg = log --graph -10 --format='%C(yellow)%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
That is … a mouthful. We’ve already gone over the --graph
option, and -10
is just shorthand for -n 10
meaning “give me 10 commits”1.
The last argument, --format
—shorthand for --pretty=format:
—is where the magic happens, so let’s go through it step by step:
%C(yellow)
– make output after this yellow%h
– print the short commit hash, just like using--abbrev-commit
option%Creset
– reset the output color%s
– the subject, aka the actual commit message%Cgreen
– make output after this green (green has its own default option)%cr
– the commit date in relative format:19 hours ago
,6 weeks ago
, etc. The parentheses around%cr
are just for visual formatting, they have no effect.%C(bold blue)
– make output after this bold and blue%an
– the author’s name. The<
and>
around it are just formatting I added for easier visual parsing, they don’t have any function.%Creset
– reset the color. This one isn’t strictly necessary since this is the end of the command, but it feels better to clean up.
So, in English, this custom formatter says:
- Print the commit hash in yellow
- Print the commit message in default color
- Print the relative date of the commit in green surrounded by
()
- Print the author’s name in bold and blue surrounded by
<>
Git log’s format has lots of options, and you can see them all under the “Pretty Formats” section in the docs. You can use those options to create exactly the log output you want.
Or you can just steal mine, it’s in an easy-to-copy-and-paste code box.
Notes
- This command will output 10 commits by default, but you can override it via
git lg -5
orgit lg -25
to get exactly the number you want. Shoutout to the fine folks at pythonbytes.fm for calling this out. ↩︎