How to make git log pretty?
You can make the log of commits displayed in commit pretty using git log –pretty=format command. You can use each of the options available in this command for each field in the log and make the log formatted and pretty.
Let us start with an existing GitHub repository with some basic options and slowly build upon it.
Consider this initial setup where the branch is main. git log –oneline is the command that can list commits without the full log description and code changes. We can see that there are twelve commits in this branch.
~/repoone$ git branch
* main
~/repoone$ git log --oneline
d0978f9 (HEAD -> main, origin/main, origin/HEAD) Sixth line in newfile
9870df7 Update newfile
27658cf Fourth line added in newfile
bead66e Another change to new file
3f54558 This is new file
4632c0d Adding empty directory
908d6ac Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit
The most straightforward formatting possible is to use –pretty=oneline, which is almost equivalent to git log –oneline. It prints all the commits in one line with just the essential fields.
~/repoone$ git log --pretty=oneline
d0978f91e9ce6c4a2368ba6c4463e5ac41eb155c (HEAD -> main, origin/main, origin/HEAD) Sixth line in newfile
9870df785ad166f8da826ad862288841b9663667 Update newfile
27658cf1c79a4d05f19e5715ec9d8e9e89ed3658 Fourth line added in newfile
bead66ed2586a8d3577d493313e921529dff28bb Another change to new file
3f54558c62250fdc07ac52a463081d8ddc4d5f88 This is new file
4632c0d520218e90d6f6645e80773d9b7e8ab0f7 Adding empty directory
908d6ac779f38123751ddd70d6107bb43cc832a1 Third commit in main
42296d0c4e8f0a61f3948dab14f1518a1a05b348 Second commit in main
453424edeed5c7c668cf7ce5d9f3b4ccb9aa566e This is first commit
2673111c4500cd406b587c1c067c9bfbae707e86 Initial commit
Now, let us see each of the arguments of git log –pretty-format specific to each field starting from a single field such as commit id. And slowly build upon it.
%h would print the commit ids only. Use git log –all –pretty-format: “%h” to print all the commit ids alone.
~/repoone$ git log --all --pretty=format:"%h"
cb1a2da
d0978f9
9870df7
aba15bf
27658cf
bead66e
3f54558
4632c0d
21f1723
908d6ac
42296d0
453424e
2673111
Now, let us add %cd, which is the committer date field. You can see that the output contains both the commit id and date. And the format is pretty compared to the extended log format.
~/repoone$ git log --all --pretty=format:"%h %cd"
cb1a2da Sun Oct 16 08:20:06 2022 +0530
d0978f9 Sun Oct 16 08:16:58 2022 +0530
9870df7 Sun Oct 16 08:11:20 2022 +0530
aba15bf Sun Oct 16 08:04:11 2022 +0530
27658cf Sun Oct 16 08:03:20 2022 +0530
bead66e Sat Oct 15 21:27:12 2022 +0530
3f54558 Sat Oct 15 21:25:35 2022 +0530
4632c0d Sat Sep 24 13:04:50 2022 +0000
21f1723 Sun Sep 11 11:52:29 2022 +0530
908d6ac Sat Sep 10 05:15:28 2022 +0000
42296d0 Sat Sep 10 05:14:40 2022 +0000
453424e Sun Sep 4 17:43:08 2022 +0000
2673111 Sat Sep 3 12:58:29 2022 +0530
You can add %s to get the description field as shown.
~/repoone$ git log --all --pretty=format:"%h %cd %s"
cb1a2da Sun Oct 16 08:20:06 2022 +0530 prinfo file first line
d0978f9 Sun Oct 16 08:16:58 2022 +0530 Sixth line in newfile
9870df7 Sun Oct 16 08:11:20 2022 +0530 Update newfile
aba15bf Sun Oct 16 08:04:11 2022 +0530 New line in repotwo README
27658cf Sun Oct 16 08:03:20 2022 +0530 Fourth line added in newfile
bead66e Sat Oct 15 21:27:12 2022 +0530 Another change to new file
3f54558 Sat Oct 15 21:25:35 2022 +0530 This is new file
4632c0d Sat Sep 24 13:04:50 2022 +0000 Adding empty directory
21f1723 Sun Sep 11 11:52:29 2022 +0530 Initial commit
908d6ac Sat Sep 10 05:15:28 2022 +0000 Third commit in main
42296d0 Sat Sep 10 05:14:40 2022 +0000 Second commit in main
453424e Sun Sep 4 17:43:08 2022 +0000 This is first commit
2673111 Sat Sep 3 12:58:29 2022 +0530 Initial commit
%an would show the author field. In addition, we have used braces to get it in brackets.
~/repoone$ git log --all --pretty=format:"%h %cd %s (%an)"
cb1a2da Sun Oct 16 08:20:06 2022 +0530 prinfo file first line (codeversionmaster)
d0978f9 Sun Oct 16 08:16:58 2022 +0530 Sixth line in newfile (codeversionmaster)
9870df7 Sun Oct 16 08:11:20 2022 +0530 Update newfile (codeversionmaster)
aba15bf Sun Oct 16 08:04:11 2022 +0530 New line in repotwo README (codeversionmaster)
27658cf Sun Oct 16 08:03:20 2022 +0530 Fourth line added in newfile (codeversionmaster)
bead66e Sat Oct 15 21:27:12 2022 +0530 Another change to new file (codeversionmaster)
3f54558 Sat Oct 15 21:25:35 2022 +0530 This is new file (codeversionmaster)
4632c0d Sat Sep 24 13:04:50 2022 +0000 Adding empty directory (Code Version Master)
21f1723 Sun Sep 11 11:52:29 2022 +0530 Initial commit (codeversionmaster)
908d6ac Sat Sep 10 05:15:28 2022 +0000 Third commit in main (codeversionmaster)
42296d0 Sat Sep 10 05:14:40 2022 +0000 Second commit in main (codeversionmaster)
453424e Sun Sep 4 17:43:08 2022 +0000 This is first commit (codeversionmaster)
2673111 Sat Sep 3 12:58:29 2022 +0530 Initial commit (codeversionmaster)
We can use %ad instead of %cd to get the author-date instead of the committer date. Also, we kept the author as the second field instead of in braces at the end. It is just to show you can rearrange the fields as you like to format them.
~/repoone$ git log --pretty=format:"%h %an %ad %s"
d0978f9 codeversionmaster Sun Oct 16 08:16:58 2022 +0530 Sixth line in newfile
9870df7 codeversionmaster Sun Oct 16 08:11:20 2022 +0530 Update newfile
27658cf codeversionmaster Sun Oct 16 08:03:20 2022 +0530 Fourth line added in newfile
bead66e codeversionmaster Sat Oct 15 21:27:12 2022 +0530 Another change to new file
3f54558 codeversionmaster Sat Oct 15 21:25:35 2022 +0530 This is new file
4632c0d Code Version Master Sat Sep 24 13:04:50 2022 +0000 Adding empty directory
908d6ac codeversionmaster Sat Sep 10 05:15:28 2022 +0000 Third commit in main
42296d0 codeversionmaster Sat Sep 10 05:14:40 2022 +0000 Second commit in main
453424e codeversionmaster Sun Sep 4 17:43:08 2022 +0000 This is first commit
2673111 codeversionmaster Sat Sep 3 12:58:29 2022 +0530 Initial commit
You can use %x00, %x01, etc., to include a delimiter character between each field. Here, we used %x09 to insert tab space. As you can see, it got more pretty formatted.
~/repoone$ git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
d0978f9 codeversionmaster Sun Oct 16 08:16:58 2022 +0530 Sixth line in newfile
9870df7 codeversionmaster Sun Oct 16 08:11:20 2022 +0530 Update newfile
27658cf codeversionmaster Sun Oct 16 08:03:20 2022 +0530 Fourth line added in newfile
bead66e codeversionmaster Sat Oct 15 21:27:12 2022 +0530 Another change to new file
3f54558 codeversionmaster Sat Oct 15 21:25:35 2022 +0530 This is new file
4632c0d Code Version Master Sat Sep 24 13:04:50 2022 +0000 Adding empty directory
908d6ac codeversionmaster Sat Sep 10 05:15:28 2022 +0000 Third commit in main
42296d0 codeversionmaster Sat Sep 10 05:14:40 2022 +0000 Second commit in main
453424e codeversionmaster Sun Sep 4 17:43:08 2022 +0000 This is first commit
2673111 codeversionmaster Sat Sep 3 12:58:29 2022 +0530 Initial commit
You can refer to all the options possible in the official documentation and play around with them as you like to format.