git: pretty one line logs

2020-10-26

 | 

~3 min read

 | 

588 words

Back when I was first learning git, one of the tricks I found was a fantastic alias for information dense git log, which I added to my .zshrc (though an alias within a .gitconfig would have also worked) and have been using a variant of ever since:

.zshrc
alias gl="git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"

This produces a nice summary view that includes information --pretty=oneline doesn’t - specifically a date of when the commit occurred and the name of the author (as defined in his/her .gitconfig). For example, here’s a printout from a personal project:

*   ef10842 2020-11-01 | Merge pull request #390 from stephencweiss/deps (HEAD -> master, origin/master, origin/HEAD) [Stephen Weiss]
|\
| * c0400b8 2020-11-01 | chore: update content (origin/deps, deps) [Stephen]
|/
*   48f71d7 2020-10-31 | Merge pull request #389 from stephencweiss/ispublished [Stephen Weiss]
|\
| * 5af30d1 2020-10-31 | feat: manual redirects + antilibrary (origin/ispublished, ispublished) [Stephen]
| * 28d628a 2020-10-31 | fix: redirects [Stephen]
|/
...

More recently, however, I came across references to git lol and git lola - apparently rather common aliases to achieve a similar aim.1

From Conrad Parker’s git lola post, the setup could look like this:

.gitconfig
[alias]
        lol = log --graph --decorate --pretty=oneline --abbrev-commit
        lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
[color]
        branch = auto
        diff = auto
        interactive = auto
        status = auto

The advantage of this approach is the colors. Specifically, --pretty=oneline has built in colors for the format, as long as they’re configured within the .gitconfig.

On the other hand, gl doesn’t. I decided to learn how to remedy that!

Fortunately, the answer is in the documentation (even if it wasn’t immediately clear how to use it)!

Creset

reset color

%C(…​)

color specification, as described under Values in the “CONFIGURATION FILE” section of git-config[1]. By default, colors are shown only when enabled for log output (by color.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal). %C(auto,...) is accepted as a historical synonym for the default (e.g., %C(auto,red)). Specifying %C(always,...) will show the colors even when color is not otherwise enabled (though consider just using --color=always to enable color for the whole output, including this format and anything else git might color). auto alone (i.e. %C(auto)) will turn on auto coloring on the next placeholders until the color is switched again.

After a bit of head scratching and experimentation, I found the a rather simple solution to my problem:

  1. Turn all colors to auto by using the ui key in the .gitconfig
  2. Wrap my format with %C(auto) and %Creset

Let’s start by updating the .gitconfig

.gitconfig
[color]
-    branch = auto
-    diff = auto
-    interactive = auto
-    status = auto
+    ui = auto

Next, update the gl alias in .zshrc:

.zshrc
alias gl="git log --pretty=format:'%C(auto)%h %ad | %s%d [%an]%Creset' --graph --date=short"

To see the difference, a screen shot is necessary.

Before:

original

After:

new

Ahhhh… sweet success: pretty colors and I learned something new!

Footnotes


Related Posts
  • Git: Search Commits By Date, Branch, Author
  • Writing Better Commit Messages


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!