DevOps Certification Training Course
- 176k Enrolled Learners
- Weekend/Weekday
- Live Class
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
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.
Pretty print commit log in a ‘single line’
Command: git log --pretty=oneline
Formats the output in sequence: <sha1> <refnames> <commit title>
Format commit output ‘short’ in the format:
commit <sha1> (refname)
Author: <author>
<title line>
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 --pretty=full
Output is in the format:
commit<sha1> (refname)
Author: <author>
Commit: <committer>
<title line>
<full commit message>
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 --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 --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>
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
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.
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 --relative-date
Kindly note, this highlighted time is subjected to change with reference to the time you execute the command on your system.
You can also format the commit logs date in any of the following format options:
Command :git log --date=relative
This is synonymous with the above command “git log --relative-date
” and prints the same commits.
Command: git log --date=local
Command: git log --date=iso
Command: git log --date=iso-strict
Command: git log --date=rfc
Command: git log --date=short
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 --date=human
Shows the date as unix epoc (UTC) time.
Command: git log --date=unix
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.
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.
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.
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.
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 ‘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.
Course Name | Date | Details |
---|---|---|
DevOps Certification Training Course | Class Starts on 31st August,2024 31st August SAT&SUN (Weekend Batch) | View Details |
DevOps Certification Training Course | Class Starts on 14th September,2024 14th September SAT&SUN (Weekend Batch) | View Details |
DevOps Certification Training Course | Class Starts on 23rd September,2024 23rd September MON-FRI (Weekday Batch) | View Details |
edureka.co