Creating a more human-readable Git history
Jon Smoley
ยท
Senior Software Engineer
Published on Wednesday Oct 4, 2023
Everyone knows that to be a REAL programmer, you have to use the command line. GitLens? GitKraken? That won't impress your crush. To do that, you need to use the Git CLI. But just because you're using the command line doesn't mean that you have to put up with the superfluous text it generates.
Chances are when you check your Git history, the output looks like this:
One of the most used commands, git log
, is famous for printing lots of text when 90% of the time, you just want to see the past couple commits. You don't need all of that extra information like the full Git hash (short hashes work in most cases), the exact second a commit happened, or the timezone offset. Sometimes, less is more.
To reduce this bloat, you might try creating a separate executable that runs your Git commands, parses the output, and has a field day with grep
and sed
. What if I told you Git already supports similar behavior, and requires nothing more than a config file that you already have on your computer?
Git Aliases
Git aliases work similarly to bash aliases, just with a narrower use case. You define an alias to map to an existing Git command (or in some cases, an external command), and Git will execute it. Aliases can be defined either via the Git CLI itself, or be hardcoded into your ~/.gitconfig
. The two following examples are equivalent and will execute the same $ git checkout
command when the user types $ git co
.
1$ git config --global alias.co checkout
1# ~/.gitconfig
2
3[alias]
4 co = checkout
The rest of this article will focus on customizing an alias for the git log
command, and show how to use the supported color and style formatting to change the output to a much more readable output, like this:
Styling your output
While we don't get full RGB support, Git supports nine different colors: normal
, black
, red
, green
, yellow
, blue
, magenta
, cyan
, and white
. The normal
colors refers to your console default.
On top of that, you can also specify one or more styles: bold
, dim
, ul
, blink
, reverse
, italic
, and strike
.
And as you can imagine, we can get pretty crazy with mixing and matching
Formatting strings
To display data from Git, find the flag that corresponds to the data you want. Just a few examples of the numerous flags :
%an
- author name%ae
- author email%cn
- comitter name- and many more...
Putting it all together
Each piece of data receives it's style using %C
, in the following format:
%C(<your-style>)<git-flag>
So a red/italic styling of a human-readable styled author date (%ah
) would be
%C(red italic)%ah
Then, once we add that to the --pretty=format
flag in out Git config...
1# ~/.gitconfig
2
3[alias]
4 nemo = log \
5 --oneline \
6 --pretty=format:'%C(red italic)%ah'
We get this!
One last thing
When you are formatting colors, the %C
utility will apply formatting until you tell it to either stop, or change. This won't be a problem if you style everything that you print, but if you have five fields and only want to style the very first field, then you will see the styling apply to all five fields. Adding a %reset
after the first call to %C
will prevent your styling from bleeding over.
Conclusion
And that's it! Now that you know how to pick data from git log
and how to style it, you can go out and create some beautiful masterpieces.
As a bonus, here is the configuration I personally use.
1# ~/.gitconfig
2
3# Create an alias under the `[alias]` header.
4# I chose 'hs' as short for "history"
5[alias]
6 hs = log \
7 --oneline \
8 --pretty=format:'%C(magenta)%h %C(green)%cd %C(bold cyan)%d %Creset%C(white)%s - %C(italic dim white)%aN' \
9 --date=format:'%b %d %Y'