The letter A styled as Alchemists logo. lchemists
Published March 15, 2020 Updated March 15, 2020

Git Log Pretty

Demonstrates displaying your Git history in an concise and easy to read manner.

Transcript

# Hello and welcome to the Alchemists Screencasts!
# Today, we'll learn about using pretty Git Logs.
# When first learning to read Git logs, common usage is:

git log

# Unfortunately, that output is too verbose.
# Your time is valuable so let's modify the output for improved readability.

gl

# By the way, `gl` is short for *git log*.
# I find this format better because we get the following via a single line:
# - What (i.e. SHA and subject).
# - Who (i.e. author).
# - When (i.e. time since the commit was created).
# Here's the source code:

cype gl
cype _git_log_line_format

# I use a seperate Git log format function for reuse in other functions.
# 💡 To see more aliases and functions, go here: https://github.com/bkuhlmann/dotfiles.
# If we put the above together as a single command, we get:

git log --graph --pretty=format:"%C(yellow)%h%C(reset) %G? %C(bold blue)%an%C(reset) %s%C(bold cyan)%d%C(reset) %C(green)%cr.%C(reset)"

# If you haven't seen any of this before, here's the breakdown:

git log --graph

# The `--graph` option shows the commit graph (handy in a Git Merge Workflow).
# In this case, the history is linear so all commits are at the same level.
# 💡 Use the Git Rebase Workflow to avoid merge bubbles and complicated histories.

# The `--pretty` option takes a `format` and is a nice way to customize your logs.
# For more information, see: https://git-scm.com/docs/git-log#_pretty_formats.
# Let's walk through the code together from left to right:

git log --pretty=format:"%C(yellow)%h%C(reset)"

# The use of `%C` defines the color we want to use.
# We specify the colors in parenthesis (i.e. `(yellow)`).
# Colors must be reset after use to prevent bleeding (i.e. `%C(reset)`).
# Finally, `%h` prints an abbreviated commit SHA hash.
# I've found an abbreviated hash of 12 characters works best while still unique:

git config --get core.abbrev

# Next is `%G?` for signed commits, printed in white color:

git log --pretty=format:"%G?"

# The output of "N" means each commit has no signature.
# I sign tags but not commits -- Tags are where the most value is.
# If a commit was signed and valid, a "G" would print.
# A bad commit signature would end up as "B".
# Lots of info is packed in these characters so check the docs for details.
# Moving on, we have `%an` for author name in a blue color:

git log --pretty=format:"%C(bold blue)%an%C(reset)"

# The author is then followed by `%s` which is the subject in white color:

git log --pretty=format:"%s"

# After the author comes the ref name (`%d`) in cyan color:

git log --pretty=format:"%C(bold cyan)%d%C(reset)"

# The ref name is extremely handy for branch, tag, etc. contextual information.
# Not every commit will have a ref name which is why the last two lines are blank.
# Finally, `%cr` is used to show time since the commit was created:

git log --pretty=format:"%C(green)%cr.%C(reset)"

# Knowing amount of time since the commit was created improves readability.
# Also less thinking on your part.
# Again, your time is precious so let Git do the heavy lifting. 😉
# When we put this all together, the following should make more sense now:

git log --pretty=format:"%C(yellow)%h%C(reset) %G? %C(bold blue)%an%C(reset) %s%C(bold cyan)%d%C(reset) %C(green)%cr.%C(reset)"

# Even better when you wrap this in an alias:

gl

# Feel free to experiment with your own Git logs.
# Use what I've shown above or customize for what works for you.

# Enjoy!
# https://alchemists.io
# ☿ 🜔 🜍 🜂 🜃 🜁 🜄