Getting started with Git logs (Part -1)

Reading Time: 4 minutes

Getting started with Git logs (Part -1)

In this blog, we will learn some advanced options to format and print the commit git logs to fetch the information that we need out of our project journal history. As we already know, Git keeps a Journal of the changes committed to the project history. Now we will explore more ways the ‘git log’ command is helpful for us.

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

$cd <my_project> – Switch to the git project

$git checkout <another_branch> – jump to the ‘feature1’ branch

1. Commit Formatting for git logs

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

Conclusion

I have tried to cover basic commands. This is the first blog on this topic you can refer to the below link to learn more about this topic.

Resource – https://www.edureka.co/blog/git-format-commit-history/

Written by 

Mohd Muzakkir Saifi is a Software Consultant at Knoldus Software. He loves to take deep dives into cloud technologies & different tools. His hobbies are playing gymnastics and traveling.