• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Computing / Improve Git Log

Improve Git Log

Last updated on January 29, 2021 by Sal Ferrarello

By running the following from the command line, you can create a new Git command (git lg).

git config --global alias.lg "log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"

This new command creates a much more useful log of Git commits.

I use Git a lot and by default git log is not very helpful. Git log has a lot of optional parameters, which can make the output much more useful.

Examples

Default Git Log

Default Git Log Output

Improved Git Log

Using a combination of command line parameters, the git log output becomes beautiful and infinitely more useful.

git log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'

Improved Git Log

Features of Improved Git Log

  • each commit uses only one line (allowing a much deeper look into project history)
  • commit IDs are displayed in abbreviated format
  • branching is visually indicated
  • colorized
  • summary line of commit is displayed
  • name of committer is displayed

How the Command Works

Much of the command is adding colors (see Git pretty formats colors). When the colors are stripped out the command becomes

git log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%h -%d %s (%ad) <%an>'
  • --color enables colors
  • --graph draws a text-based graphical representation of the commit history on the left hand side of the output
  • --date=format: '%Y-%m-%d %H:%M:%S' defines the date format to use (e.g. 2019-12-01 12:45:51)
  • --pretty=format:'%h -%d %s (%ad) <%an>' A custom pretty format where we use the following placeholders:
    • %h abbreviated commit hash
    • - a dash
    • %d ref names (e.g. branch names, tag names), note: when there is output this also outputs a leading space
    • %s subject (first line of commit message)
    • %ad author date (format respects --date= option which we set earlier)
    • %an author name

git lg

While the Improved Git Log is amazing, typing out the list of parameters each time would be ridiculous. Fortunately, Git allows us to create our own aliases. In this case, we’re going to create an alias to git lg. By default if you run git lg, you’ll get the message

git: ‘lg’ is not a git command. See ‘git –help’.

However, using the command mentioned at the beginning of this article we can add the alias to our global configuration. Here it is again.

git config --global alias.lg "log --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"

Now, every time we want the improved git log, we can type git lg.

How the Alias Works

The git config --global line above writes this new alias to your .gitconfig file (which is found at ~/.gitconfig on a Mac).

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Computing, Programming, Solution Tagged With: Git, git alias, gitconfig

Reader Interactions

Comments

  1. Billy says

    October 10, 2018 at 12:34 pm

    you forgot an ending double quote on your last code snippet.

    Reply
    • Sal Ferrarello says

      October 10, 2018 at 1:48 pm

      Hi Billy,

      Thank you for catching the missing closing double quote. I’ve now added it.

      Thanks again.

      Reply
  2. Robert says

    September 18, 2020 at 8:54 am

    Very nice, thanks!

    Got this set up locally with some minor changes. I found you can use %C(auto)%d%Creset to restore the standard “decorate” colors.

    BTW: The screenshot shows dates in relative format rather than %Y-%m-%d %H:%M:%S. I guess some other (non-default) setting is overriding that.

    Reply
  3. Jorge says

    April 7, 2022 at 12:00 pm

    This is wonderful! Thank you 🙂

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in