Thursday, 8 August 2019

GIT LOG COMMAND EXPLAINED WITH EXAMPLES

INTRODUCTION

In our previous articles on working with the git version control system, we explained how to initialize a local git repositoryadd content to it and we also worked with the git add in detail. In this article, we will focus on the git log command. We will understand the significance of the git log command, the information it provides and usage examples. The significance of git log The git log command allows us to read and review a history all the changes and updates that place in a git repository. This is essential in tracking changes that were made to a file. The git log command also provides us the required information to revert to a previous version of the file or at times even recover a deleted file. We will talk in greater detail about reverting to previous versions of files and deleted file recovery in a subsequent article. Information provided by ‘git log’ A typical git log will provide you with information about all the commits made to a git repository since its inception. It contains the following details corresponding to each commit:
  • A commit hash: A SHA1 40 character checksum of the commits’ contents. Since it is generated based on the commit contents it will always be unique.
  • Commit Author metadata: The name and email address of the author of the commit.
  • Commit Date metadata: A date timestamp for the time of the commit.
  • Commit title/message: The description or comment which was written by the author during the git commit operation.
git log command examples: Now that we understand what the git log command is and the various fields of output it contains, let’s a look at a couple of examples to familiarize ourselves with the usage of this command. We would like to point out that git log will probably be one of the most frequently used git related commands that you are likely to come across. Continuing with our repository created in /home/sahil/git/my_first_repo, I’ve made a couple of more commits since we last used it.
Example 1: Basic usage without additional options In the simplest form of using the git log command, we invoke it without any additional options. Given below is an example:
[sahil@linuxnix my_first_repo]$ git log
commit 23c977041acf1730accd0622ad47b71a6a40a32a
Author: Sahil Suri <sahil.suri@example.com>
Date: Fri Mar 9 12:31:20 2018 +0530

Added another line to test.txt

commit a291f69bcdd40b9f9bf409cafe51030ab9b67e38
Author: Sahil Suri <sahil.suri@example.com>
Date: Tue Mar 6 18:30:54 2018 +0530

Added file test.txt nad updated .md files

commit d76dd618aff0358ea0a63a8a67f398ffe231e775
Author: Sahil Suri <sahil.suri@example.com>
Date: Tue Mar 6 18:20:15 2018 +0530

Modified the *.md files
-------------output truncated for brevity
Note: As you continue to work on the repository and make additional commits, the output of the git log command may become too long to fit on a single terminal screen. When this happens the output of the git log command becomes automatically paginated using the less paging utility.
Example 2: Summarized output In case you are not interested in the author and timestamp details for the commit history, you can shorten the git log command output to a summary type output using the –oneline option with the git log command.
[sahil@linuxnix my_first_repo]$ git log --oneline
23c9770 Added another line to test.txt
a291f69 Added file test.txt nad updated .md files
d76dd61 Modified the *.md files
99fa732 Created a new file README1.md
f9849b2 Added a line to README.md
f7eccb7 Added my first file
This gives the first seven characters of the SHA1 hash corresponding to the git commit along with the comment or description message saved with the commit.
Example 3: View only recent commits We can use the git log command to view only the most recent commits by typing how far back in the history we want git to go while displaying its output. In the below example, we instruct git log to limit it’s output and display information about the last two commits only.
[sahil@linuxnix my_first_repo]$ git log -2
commit 23c977041acf1730accd0622ad47b71a6a40a32a
Author: Sahil Suri <sahil.suri@example.com>
Date: Fri Mar 9 12:31:20 2018 +0530

Added another line to test.txt

commit a291f69bcdd40b9f9bf409cafe51030ab9b67e38
Author: Sahil Suri <sahil.suri@example.com>
Date: Tue Mar 6 18:30:54 2018 +0530

Added file test.txt nad updated .md files
Example 4: Filter commits by author This option is useful if you have many people working on the same repository. To use this option, we type git log followed by the keyword author and the authors’ name whom we want to look for. Given below is an example.
[sahil@linuxnix my_first_repo]$ git log --author="Sahil" --oneline
23c9770 Added another line to test.txt
a291f69 Added file test.txt nad updated .md files
d76dd61 Modified the *.md files
99fa732 Created a new file README1.md
f9849b2 Added a line to README.md
f7eccb7 Added my first file
For this repository, since all the commits were made by me, we get the entire commit history.
Example 5: Filter output by the time We can filter the git log command output by time in case we need to view commits made after or before a certain point in time. We use the –after and –before keywords with the git log command followed by the time window generally specified in days. Here are two examples.
[sahil@linuxnix my_first_repo]$ git log --oneline --before 2.days.ago
a291f69 Added file test.txt nad updated .md files
d76dd61 Modified the *.md files
99fa732 Created a new file README1.md
f9849b2 Added a line to README.md
f7eccb7 Added my first file
[sahil@linuxnix my_first_repo]$
[sahil@linuxnix my_first_repo]$ git log --oneline --after 1.day.ago
23c9770 Added another line to test.txt
[sahil@linuxnix my_first_repo]$
In the first example, we view commits that were made more than two days ago and in the second example, we view commits that were made less than a day ago.
Example 6: Filter commits by date range We could also filter the git log command output to view commit messages made during a range of time. In this case, we would use the –after and –before keywords together in the same command. Given below is the syntax for using the git log command to filter its output within a certain date range.
git log --after <date> --before <date>
Given below is an example:
[sahil@linuxnix my_first_repo]$ git log --oneline --before "2018-03-09" --after "2018-03-07"
23c9770 Added another line to test.txt
[sahil@linuxnix my_first_repo]$
In the above example, we filtered the commit messages within the range of 7th March to 9th March for the year 2018.
Example 7: Formatting the git log output We can use the –pretty option with the git log command to significantly modify how the output would appear on the screen. This includes the addition of colors to the output. The syntax for using –pretty with the git log command is as follows:
git log --pretty=format:"<options>"
Let’s take a look at an example.
git log --pretty=format:"Commit Hash: %Cred%H%Creset, Author: %Cblue%aN%Creset, Date: %Cgreen%aD%Creset"
As you may observe the output is neatly formatted and colored which helps make the output more readable.

CONCLUSION

In this article, we explained the importance of the git log command and demonstrated its usage with the help of examples. 

0 comments:

Post a Comment