AWS Global Infrastructure

DevOps

Topics Covered
  • DevOps (75 Blogs)
  • Mastering Git and GitHub (4 Blogs)
  • Docker (9 Blogs)
  • DevOps Engineer Masters Program (7 Blogs)
SEE MORE

How to use Git Log to format the commit history?

Last updated on Apr 22,2020 23.8K Views

Divya Bhushan
A passionate Content Developer with key skills – Git, Dockers, Databases, Linux... A passionate Content Developer with key skills – Git, Dockers, Databases, Linux and Unix.
8 / 10 Blog from Git & GitHub

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>


Commit - Git log format history - Edureka

1.1.2 –pretty=short

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

<title line> 
Commit command - Git log format history - Edureka

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>
Command- Git log format history - Edureka

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>
Command- Git log format history - Edureka

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>
Command- Git log format history - Edureka

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>
Command- Git log format history - Edureka

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>

Pretty command- Git log format history - Edureka

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]

Command- Git log format history - Edureka

%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

Command- Git log format history - Edureka
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
Command- Git log format history - Edureka

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.


Command- Git log format history - Edureka       

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

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


Command- Git log format history - Edureka
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


Command- Git log format history - Edureka

1.4 –relative-date

Command: git log --relative-date
Command- Git log format history - Edureka
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
Command- Git log format history - Edureka

1.5.3 –date=iso

Command: git log --date=iso
Command- Git log format history - Edureka

1.5.4 –date=iso-strict

Command: git log --date=iso-strict
Command- Git log format history - Edureka

1.5.5 –date=rfc

Command: git log --date=rfc
Command- Git log format history - Edureka

1.5.6 –date=short

Command: git log --date=short
Command- Git log format history - Edureka

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.Command- Git log format history - Edureka

1.5.8 –date=human

Command: git log --date=human
Command- Git log format history - Edureka
Command- Git log format history - Edureka

1.5.9 –date=unix

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

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

Command- Git log format history - Edureka

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

Command- Git log format history - Edureka

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

Command- Git log format history - Edureka
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.

Command- Git log format history - Edureka
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 format history - Edureka

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

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"
Command- Git log format history - Edureka
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 ‘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’re curious to learn more you can check out this DevOps Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The Edureka DevOps Certification Training course helps learners to understand what is DevOps and gain expertise in various DevOps processes and tools such as Puppet, Jenkins, Nagios, Ansible, Chef, Saltstack and GIT for automating multiple steps in SDLC.

Got a question for us? Please mention it in the comments section of this article on “Git log format history” and we will get back to you.

Upcoming Batches For DevOps Certification Training Course
Course NameDate
DevOps Certification Training Course

Class Starts on 10th September,2022

10th September

SAT&SUN (Weekend Batch)
View Details
DevOps Certification Training Course

Class Starts on 12th September,2022

12th September

MON-FRI (Weekday Batch)
View Details
DevOps Certification Training Course

Class Starts on 8th October,2022

8th October

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to use Git Log to format the commit history?

edureka.co