Git protip: a graphical log that rocks!
By Maxime Bréhin • Published on 29 August 2022
• 2 min
The log is a Git tool that displays the commit history of a project. We usually use it to:
- display the latest commits, to know how far along we are in our work;
- visualize the current branches and their respective histories.
Problem is, the “regular” log looks like this:
It displays commits from most recent (top) to oldest (bottom), without any branch data, but with all the details of every commit:
- full 40-character identifier,
- full message (might be several lines),
- date and name of the committer,
- and line breaks to make it breathe.
It’s rather unwieldy. As a result, many people turn to graphical interfaces (therefore adding yet another tool). And yet, Git log features a ton of options to customize its display. I’ll skip the details here to focus on our magic alias! 🧙♂️
First, let’s decide on the data and format that we would like to have in our enhanced display:
- Concise: first line of the commit message, who produced it and when, abbreviated identifier (to make it easier to re-use with other commands);
- Visual: clear graphical sequencing of commits, labels for branches, tags and our current position (HEAD).
This is achieved by the following options:
git log --abbrev-commit --graph --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'
I’m guessing you won’t want to type that every time, so you might as well set up an alias for it. We named it lg
, which is easy to type and close to log
. To set it up, just copy-paste the following line into your terminal:
git config --global alias.lg "log --graph --abbrev-commit --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'"
The rendering is much more pleasant and useful. You can still use the options. For instance, you can extend the visualisation to all branches by doing a git lg --branches
(assuming the same history as the earlier example).
More tips and tricks?
We’ve got a whole bunch of existing articlesand more to come. Also check out our 🔥 killer Git training course: 360° Git!