Ways to Show Git Logs in better way using pretty formatting


By default when you use the git log command without any options, you would see details for each commit in multiple lines with details such as

  • 40-character long commit hash (where the head is pointing)
  • Author details, username followed by the author email ID
  • Date
  • Commit comment
% git log
commit 6ac92d6dece8c15b9472da2590a23f58ff48b4f9 (HEAD -> main)
Author: c2c 
Date:   Tue Jan 24 18:14:47 2023 +0000

    4th commit

commit 06250f0b1cdb7054863f1307d6e66fa8fadda81d
Author: c2c 
Date:   Tue Jan 24 18:14:34 2023 +0000

    3rd commit

commit 27125141f5e0d692e80df0c3113d68334523e539
Author: c2c 
Date:   Tue Jan 24 14:21:48 2023 +0000

    2nd commit

commit a3e24f41e70dec79395c730d68bf26ce64dd7444
Author: c2c 
Date:   Tue Jan 24 14:21:38 2023 +0000

    1st commit

This could easily become hard to read when there are multiple branches and lots of commits.

You can easily make the commit logs to be one/line by using the option --oneline

% git log --oneline        
6ac92d6 (HEAD -> main) 4th commit
06250f0 3rd commit
2712514 2nd commit
a3e24f4 1st commit

As you may see that with --oneline option, the commit logs are readable but miss a lot of crucial information such as commit time, author, etc.


Ways to show git commit log in a better way

Example 1: Using pretty formatting

Let's try to get all the information that we need in one line with pretty format options,

% git log --graph --pretty=format:'%h - %s (%cr) - %an'

* 6ac92d6 - 4th commit (25 minutes ago) - c2c
* 06250f0 - 3rd commit (25 minutes ago) - c2c
* 2712514 - 2nd commit (4 hours ago) - c2c
* a3e24f4 - 1st commit (4 hours ago) - c2c

%h -> abbreviated commit hash

%s -> subject (commit message)

%cr -> committer date, relative

%an -> author name

Git Pretty Formatted Commit Logs

Example 2: Adding Colors

% git log --graph --pretty=format:'%Cred%h%Creset - %Cblue%s%Creset (%cr) - %Cblue%an%Creset'
* 6ac92d6 - 4th commit (36 minutes ago) - c2c
* 06250f0 - 3rd commit (37 minutes ago) - c2c
* 2712514 - 2nd commit (4 hours ago) - c2c
* a3e24f4 - 1st commit (4 hours ago) - c2c

Color Options

  1. %Cred: Set red color
  2. %Cgreen : Set green color
  3. %Cblue: Set blue color
  4. %Creset : To reset the color
Set Colors to Git Log command formatting

Example 3: Creating an alias for the log formatting in bash/zsh profile

Na! There is no way one can remember or type such huge formatting strings each time you want to see the commit logs. It's always better to come up with your preferred way of a formatting string and add them as an alias to the bash/zsh profile,

Open the .zshrc file or the .bash_profile file and create an alias like below,

alias gitlog1="alias gitlog1="git log --graph --pretty=format:'%Cred%h%Creset - %Cblue%s%Creset (%cr) - %Cblue%an%Creset'"
alias gitlog2="git log --graph --pretty=format:'%h - %s (%cr) - %an'"

Invoke custom git log alias,

 % gitlog1
* 6ac92d6 - 4th commit (44 minutes ago) - c2c
* 06250f0 - 3rd commit (44 minutes ago) - c2c
* 2712514 - 2nd commit (5 hours ago) - c2c
* a3e24f4 - 1st commit (5 hours ago) - c2c

Custom multiline log example

Use the %n option to add a newline,

% git log --graph --pretty=format:'Commit: %h %nComment: %s %nDate:%ci (%cr) %nAuthor Name: %an %nAuthor Email:%ae %nCommenter Name:%cn %nCommenter Email:%ce'

* Commit: 6ac92d6 
| Comment: 4th commit 
| Date:2023-01-24 18:14:47 +0000 (54 minutes ago) 
| Author Name: c2c 
| Author Email:c2c@c2c.com 
| Commenter Name:c2c 
| Commenter Email:c2c@c2c.com
* Commit: 06250f0 
| Comment: 3rd commit 
| Date:2023-01-24 18:14:34 +0000 (54 minutes ago) 
| Author Name: c2c 
| Author Email:c2c@c2c.com 
| Commenter Name:c2c 
| Commenter Email:c2c@c2c.com
* Commit: 2712514 
| Comment: 2nd commit 
| Date:2023-01-24 14:21:48 +0000 (5 hours ago) 
| Author Name: c2c 
| Author Email:c2c@c2c.com 
| Commenter Name:c2c 
| Commenter Email:c2c@c2c.com
* Commit: a3e24f4 
  Comment: 1st commit 
  Date:2023-01-24 14:21:38 +0000 (5 hours ago) 
  Author Name: c2c 
  Author Email:c2c@c2c.com 
  Commenter Name:c2c 
  Commenter Email:c2c@c2c.com

More Options:

  • %ci -> Commit time in ISO format
  • %cn -> Commiter name
  • %ce -> Commiter email

Also do checkout the git doc page for pretty formatting: https://git-scm.com/docs/pretty-formats/2.39.0

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap