DEV Community

Cover image for Customize your git commit history output with colors using git log 🌈
Roberto B.
Roberto B.

Posted on β€’ Edited on

10

Customize your git commit history output with colors using git log 🌈

When working with Git, git log is a powerful command showing the commit history.
The git log command supports many options and parameters to control the output, allowing you, as a developer, to decide what information you want to see. Your preferences will depend on your specific needs, but let me show you my preferred git log options.
I usually display one commit per line and use some color. Colors are helpful not only for aesthetics but also for highlighting useful information.

If you are using "git log" with no options, you will see something like this:

commit a5e4001cd88b3dd3ff374856514e20cfffe4379c (HEAD -> master, origin/master)
Author: Roberto B <your-email-address@domain.com>
Date:   Fri Jun 19 07:48:04 2020 +0200

    Clean up runtime parameters to improve readability.
Enter fullscreen mode Exit fullscreen mode

For each commit, you will see more than one line with:

  • the hash of the commit (the commit identifier), 40 characters long;
  • the ref name (if the commit is related to a branch or tag or HEAD);
  • The author with full name and email address;
  • the date of the commit in a long format;
  • the commit message.

1 commit - 1 line

If you want to see one commit per line, you need to use --pretty=oneline option:

git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode

In this case, you will see:

  • the hash of the commit, 40 characters long;
  • the ref name (if the commit is related to a branch or tag or HEAD);
  • the commit message.

If 40 characters for the hash is too long, you can use the shorter values for the hash (but also unique) with --abbrev-commit option:

git log --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

Is it better?

Let's customize the format

If you want to customize the output, you can use the format option combined with --pretty.
With --pretty=format, you can use placeholders to specify which information you want to show.
For example:

git log --pretty=format:'%h %s'
Enter fullscreen mode Exit fullscreen mode

You want to see:

  • %h: abbreviated commit hash (7 characters, like --abbrev-commit option);
  • %s: the subject of the comment of the commit.

More colors

With placeholders, you can also use colors (%C<colorname>):

For example, like the example above, if you want to see the hash commit (%h) with red color:

git log --pretty=format:'%Cred%h%Creset %s'
Enter fullscreen mode Exit fullscreen mode

With %Cred you want to use the red color. With %Creset you want to reset the default color.

More colors, more information

Usually, I use this git log configuration:

git log --pretty=format:'%Cred%h%Creset %C(bold blue)%an%Creset %Cgreen%cr%Creset %s%C(bold red)%d%Creset'
Enter fullscreen mode Exit fullscreen mode

Where:

  • %Cred%h%Creset: the hash commit (%h) in red color;
  • %C(bold blue)%an%Creset: the author name (%an) with bold blue color %C(bold blue);
  • %Cgreen%cr%Creset: the committer date relative %cr (for example, relative means: "5 minutes ago") in green color %Cgreen
  • %s: the commit message (the subject with %s placeholder);
  • %C(bold red)%d%Creset: the ref names %d in red bold color.

Customize Your Git Commit History Output with Colors Using git log

Now that you understand the basics, you can go to the official documentation at https://git-scm.com/docs/pretty-formats, start playing with all the placeholders, and customize your git log history.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil β€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging β†’

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay