How to use Git Log to format the commit history?

Kavya Tolety
Edureka
Published in
9 min readAug 15, 2019

In this article, we will discuss some advanced options to format and print the commit logs to fetch the information that you need out of your project journal history. Since we already know, Git keeps a Journal of the changes committed to the project history, we shall now explore more ways the ‘git log’ command is helpful.

Firstly, I am switching-to/checking out the “feature1” branch for a convenient and shorter history.
Use the commands –

$cd myProj–Switch to the git project

$git checkout feature1–jump to the ‘feature1’ branch

1. Commit Formatting

1.1 Pretty-print the output contents in a given format

Syntax: git log --pretty[=<format>]

where, <format> can be one of oneline, short, medium, full, fuller, email, raw, and format:<string>
When =<format> part is omitted, it defaults to medium.

1.1.1 –pretty=oneline

Pretty print commit log in a ‘single line’
Command: git log --pretty=oneline
Formats the output in sequence: <sha1> <refnames> <commit title>

1.1.2 –pretty=short

Format commit output ‘short’ in the format:
commit <sha1> (refname)
Author: <author>
<title line>

1.1.3 –pretty=medium

Command: git log --pretty=medium
Print commit output in the ‘medium’ format:
commit<sha1>
Author: <author>
Date: <author date>

<title line>

<full commit message>

1.1.4 –pretty=full

Command: git log --pretty=full
Output is in the format:
commit<sha1> (refname)
Author: <author>
Commit: <committer>

<title line>

<full commit message>

1.1.5 –pretty=fuller

Command: git log --pretty=fuller
commit<sha1> (refname)
Author: <author>
AuthorDate: <author date>
Commit: <committer>
CommitDate: <committer date>

<title line>

<full commit message>

1.1.6 –pretty=email

Command: git log --pretty=email
Print log output in the email style format:
From <sha1> <date>
From: <author>
Date: <author date>
Subject: [PATCH] <title line>

<full commit message>

1.1.7 –pretty=raw

Command: git log --pretty=raw
The raw log output format shows the entire commit exactly as stored in the commit object.
commit <sha-1>
tree <tree-sha-1>
parent <sha-1 of the previous commit object>
author <author name> <email id> <timestamp>
commit <committer name> <committer email id> <timestamp>

<title line>

<full commit message>

1.1.8 –format:<string> : Custom formatting

The format allows you to specify which information of the commit object you want to print in the commit output log
Let us consider the various placeholder this option provides just like a ‘C printf’ function with the help of code snippets:

Command: git log --pretty=format:"%h %ad | %s %d [%an]" --date=short
Output format:
<sha-1> <author date> | <commit title> <refname> [author name]

%h=Shortened hash-id/sha1commit ids
%H=long sha-1 ids
%ad=authored date
%s= commit subject title line
%d=reference pointer(branch, tag) names
%an=author name
–date=short: Print just the date and not time in a readable format

Now, how about making this output more human-friendly, using colors.
Command:
git log --pretty=format:"%C(yellow)%h%Creset %ad | %Cgreen%s%Creset %Cred%d%Creset %Cblue[%an]" --date=short

Some other placeholders used in the above code snippet are:
%C(yellow): Turn the following string to yellow
%Creset: Reset the following string back to default(white) color
%Cgreen: change following string to green
%Cred: Change the following string to red
%Cblue: Make the author name blue in color

You do not have to remember and write the whole command every time, just use a short name as git alias as shown below:
Command:
git config --global alias.c-hist 'log --pretty=format:"%C(yellow)%h%Creset %ad | %Cgreen%s%Creset %Cred%d%Creset %Cblue[%an]" --date=short'

“c-hist” represents customized-history
So, as you would have observed I am setting my global git configuration file with the values.

Now, to check the history of the current branch all you have to do is run the command, like so:
Command: git c-hist

1.2 –abbrev-commit: Shorten git commit hash-id

Command: git log --abbrev-commit
The full 40-byte hexadecimal commit object name is shortened to default 7-bytes.

Let us club it with the ‘--oneline‘ option for a convenient view, like so:
Command: git log --abbrev-commit --oneline

What’s more exciting is that you can also specify the byte length of sha-1 ids using the ‘–abbrev=<n>’ option, as shown below:
Command: git log --abbrev-commit --abbrev=5 --oneline

Clearly, the highlighted sha-1 ids are reduced to 5-byte size.

1.3 –no-abbrev-commit

Show the full 40-byte hexadecimal commit object name.
This negates –abbrev-commit and those options which imply it such as “–oneline”.
Command: git log --pretty=oneline --no-abbrev-commit

1.4 –relative-date

Command: git log --relative-date

Kindly note, this highlighted time is subjected to change with reference to the time you execute the command on your system.

1.5 –date=<format>

You can also format the commit logs date in any of the following format options:

1.5.1 –date=relative

Command :git log --date=relative
This is synonymous with the above command “git log --relative-date” and prints the same commits.

1.5.2 –date=local

Command: git log --date=local

1.5.3 –date=iso

Command: git log --date=iso

1.5.4 –date=iso-strict

Command: git log --date=iso-strict

1.5.5 –date=rfc

Command: git log --date=rfc

1.5.6 –date=short

Command: git log --date=short

1.5.7 –date=raw (shows the date in seconds)

Command: git log --date=raw
Print the time as seconds since the unix epoc time ( Jan 01 1970 ) followed by the timezone.

1.5.8 –date=human

Command: git log --date=human

1.5.9 –date=unix

Shows the date as unix epoc (UTC) time.
Command: git log --date=unix

1.6 –parents

Print also the parents of each commit in the format: <commit> <parent and/or parents>
Command: git log --parents
Oneliner output Command: git log --parents --oneline

Points to be noted:
C366419 is a merge commit, hence has 2 parents respectively: feeb30c and 4920adc
Likewise;
1d67b50 is a merge commit, that resulted from merging f2ff2e4 and abb694b
078f9f5 is a merge commit created by merging 9a2412e and ab3a5e5
Whereas, 86792c6 is the initial commit, hence no parent.

1.7 –children

Print also the children in the form <commit> <children>
Command: git log --children --oneline

Note:
006b9ce is the latest commit, hence has no children commit object yet. The next change you make and commit on this branch will be the child commit object of this latest sha-1 id.

1.8 –graph

Draw a text-based graphical representation of the commit history before the sha-1 ids.
Command: git log --graph
Improved oneliner output: git log --graph --oneline

This lets you understand when, how and why and other branches were merged into the currently checked out branch.

1.9 –show-linear-break

Command: git log --show-linear-break
This is a useful command, to indicate a barrier between 2 consecutive commits that do not belong to a linear branch, in other words the commits that came from different branches.

Compare the above output with the ‘git log –graph’ command output that clearly shows how the “linear-break” commits have been merged.

Bonus: Summarise git log output: ‘git shortlog’

The ‘git shortlog‘ command categorizes the commit logs author-wise and prints an overview summary, indicating the commits made by each author.
Command: git log shortlog

Command: git log shortlog -s
-s stands for –summary, suppress commit description and just print the count of commits by each author, like so:

Furthermore, you could also format the output using the same placeholders as discussed under ‘--pretty=format‘ option
Try the command: git shortlog --format="%h | %s"

Hence, you shall agree this output makes more sense as it shows the <sha-1> id and the <commit title> for each author along with the total commits count.

Note: It is interesting to note that you can very easily find the branch that made a particular commit. It is worth taking up this discussion in upcoming articles in depth.

So with that, we come to an end of this Git log format history blog, I hope you found it informative.

In this post, we learned some formatting techniques that print the project information in a more customized and user-friendly way. You should now know how to effectively use the parameters of the ‘git log’ command to pull out any information you need about the source code from your committed history. So with that, we come to an end of this article, I hope you found it informative.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of DevOps.

1. DevOps Tutorial

2. Git Tutorial

3. Jenkins Tutorial

4. Docker Tutorial

5. Ansible Tutorial

6. Puppet Tutorial

7. Chef Tutorial

8. Nagios Tutorial

9. How To Orchestrate DevOps Tools?

10. Continuous Delivery

11. Continuous Integration

12. Continuous Deployment

13. Continuous Delivery vs Continuous Deployment

14. CI CD Pipeline

15. Docker Compose

16. Docker Swarm

17. Docker Networking

18. Ansible Vault

19. Ansible Roles

20. Ansible for AWS

21. Jenkins Pipeline

22. Top Docker Commands

23. Git vs GitHub

24. Top Git Commands

25. DevOps Interview Questions

26. Who Is A DevOps Engineer?

27. DevOps Life cycle

28. Git Reflog

29. Ansible Provisioning

30. Top DevOps Skills That Organizations Are Looking For

30.Waterfall vs Agile

31. Jenkins CheatSheet

32. Ansible Cheat Sheet

33. Ansible Interview Questions And Answers

34. 50 Docker Interview Questions

35. Agile Methodology

36. Jenkins Interview Questions

37. Git Interview Questions

38. Docker Architecture

39. Linux commands Used In DevOps

40. Jenkins vs Bamboo

41.Nagios Tutorial

42. Nagios Interview Questions

43.DevOps Real-Time Scenarios

44.Difference between Jenkins and Jenkins X

45.Docker for Windows

46.Git vs Github

Originally published at https://www.edureka.co on August 15, 2019.

--

--

Kavya Tolety
Edureka
Writer for

Hey! Just a normal girl on the internet who loves to read, research and analyze