Introduction
"git log" is a command used in the Git version control system to display a detailed list of all the commits that have been made to a repository. It is a very useful tool for anyone who wants to keep track of the changes made to a codebase over time.
This command is used to display the History of all the Commits in the Local Repository.
It is the most commonly used command.
It’ll provide complete/detailed information on the log of all the Commits.
40 Character alphanumeric Commit ID.
Author and his / her mail id.
Timestamp
Commit message
Example
Create a directory named "project3"; And create 3 files "a.txt", "b.txt" & "c.txt" in that directory.
Now, Go to the Workspace “project3” and check the number of files. Use the command ls
for that. It’ll display the 3 files: “a.txt”, “b.txt” and “c.txt”.
Now, initialize the workspace using the command git init
After that, we'll add all three files in the Staging Area using the command git add .
Then, check which files are being tracked by GIT. For that, we can use the command git ls-files
. All three files are being tracked by the GIT.
Now, create a new file "d.txt" in that Workspace "project3" and write something in that file.
Now the new file "d.txt" will be present in the Workspace “project3”. Use the command ls
to verify that.
But it’ll not be tracked by GIT. To verify that we can use the command
git ls-files
The file "d.txt" is not present there in the tracking list (Staging Area).
To confirm that the file "d.txt" is still untracked, we’ll check the status of the GIT. For that, we can use the command git status
It’ll display that the file "d.txt" is not being tracked by GIT.
Now, we’ll add the newly created file “d.txt” to the Staging Area. For that, we can use the command git add <filename>
.
In our case, it'll be git add d.txt
Now, the file “d.txt” will be added to the Staging Area.
Now, check again which files are being tracked by GIT. For that, we can use the command git ls-files
. Now, this time all four files (“a.txt”, “b.txt”, “c.txt” and “d.txt”.) are being tracked by the GIT as we have added the file “d.txt” to the Staging Area.
Now, we’ll Commit the changes to the Local Repository. For that, we can use the command git commit -m "Commit message"
. So, now all the changes will be committed.
Now, we’ll check the status of GIT again. For that, we can use the command git status
. It’ll tell: Nothing is there to Commit, everything on the Working Tree is clean. Everything is being tracked by GIT.
Now, we’ll check all the Commits or want to check the history of all the Commits. For that, we can use the command git log
So, there are 4 commits present in the log.
Note: I've committed the file a.txt and c.txt before (In the previous Article Mastering Git: Getting Started- Part 2 (hashnode.dev))
If we want the Specific Information, not the Complete / Detailed Information so, for that there are various options available. To check that we’ll use the command git log --help
. It’ll display various options available for the git log
.
Display Log Information Of A Particular File
The Commands,
git log
andgit log .
both are the same i.e to display the Complete/Detailed Information of the Commits.So, if we want the log of a particular file then we’ll put the file name in the place of the dot (.).
Syntax
git log <filename>
In the Workspace, if we want to see the Log Information related to the file “a.txt” then we’ll put the filename (a.txt) in the place of the dot (.), in the command git log .
The command will be git log a.txt
Now, we’ll see only two commits in place of 4 commits. First, commit (Lower) is associated with both “a.txt” and “b.txt”. And the second commit (Upper) is associated with only "a.txt"
Now, if we want to see the Log Information related to the file “d.txt” then we’ll put the filename (d.txt) in the place of the dot (.), in the command git log .
The command will be git log d.txt
Now, we’ll see only one commit in place of 4 commits. It’ll be associated with only “d.txt”.
Display The Brief / Concise Log Information
To get concise Information of all the commits, we’ll use the option --oneline
along with the command git log
So, the command will be git log --oneline
This command will show all the commit Information in one line per Commit, as for commit there are multiple lines displayed when we use the command git log
.
And in the one-line information, we’ll see the information:
Commit ID and
Commit message
Note: When we use the command git log --oneline
and want to check the Commit ID then, it’ll show only the first 7 Characters.
The GIT is following the order: Latest (Upper) to Oldest (Lowest).
The Commit Id will be the same if we Move the file from Local Repository to the Remote Repository.
Display The Particular Number Of The Latest Commits
If we want to see the Particular Number Of The Latest Commits i.e 2 out of 5 commits, then we can use the option -n
along with the command git log
Syntax
git log -n <number of the latest commit to be displayed>
This command will limit the number of Commits to display.
Example 1
If we want to see the 2 latest Commits to be displayed then we can use the command git log -n 2
It’ll display 2 out of 4 commits that are present in the Local Repository.
Example 2
If we want to see the latest Commits not in Detailed Information, but in concise Information, then we can use both options -n (Used First) and the --oneline
(Used Second) along with the command git log
Syntax
git log -n <number of the latest commit to be displayed> --online
It’ll display the n latest Commits out of m number of commits that are present in the Local Repository and share concise Information.
In our case, if we want to see the two latest commits, then the command will be git log -n 2 --oneline
If we want we can remove the option -n from the command git log -n <number of the latest commit to being displayed>
Instead we can use
git log -<number of the latest commit to being displayed>
If we want to see the 2 latest Commits to be displayed then we can use the command git log -2
It’ll display 2 out of 4 commits that are present in the Local Repository.
Just like before we can remove the option -n while displaying the Concise Information. We can use the command
git log -<number of the latest commit to being displayed> --online
I
t’ll also display the n latest Commits out of m number of commits that are present in the Local Repository and share concise Information.
In our case, if we want to see the two latest commits, then the command will be git log -2 --oneline
Example 3
We can use the option --max-count
along with the command git log
git log --max-count=<number of the latest commit to be displayed>
This command will also limit the number of Commits to display.
If we want to see the 2 latest Commits to be displayed then we can use the command git log --max-count=2
It’ll display 2 out of 4 commits that are present in the Local Repository.
In our case, if we want to see the two latest commits, then the command will be git log --max-count=2
Example 4
If we want to see the latest Commits not in Detailed Information, but in concise Information, then we can use both the option --max-count
(Used First) and the --oneline
(Used Second) along with the command git log
git log --max-count=<number of latest commit to display> --online
In our case, if we want to see the two latest commits, then the command will be git log --max-count=2 --oneline
It’ll display the n latest Commits out of m number of commits that are present in the Local Repository and share concise Information.
So, the commands:
git log -n <number of the latest commit to be displayed>
git log -<number of the latest commit to be displayed>
git log --max-count=<number of the latest commit to display>
All three commands are the same
Display Commits Based On The Commit Message
If there is a project in which too many changes/Commits are made, few of them are to solve a particular issue. Example: "Issue in Service Request 123".
And we have also put the Commit Message related to that particular issue. So, if new developers start working on the Issue in Service Request 123, then they don’t have to go through the whole Commits.
Instead, they can search/filter the Commits based on the Issue in Service Request 123 using the String in the Commit Message.
Example
If we have Changed/Modified/Added something in the Service Request Add a line in the Service Request 123, then we’ll put a meaningful/Structured Message for that while Committing and essentially use the term "Service Request 123", so the developer will use that string to display/filter the whole commits based on the Service Request 123.
For this, the developer / we can use the option --grep
along with the command git log
.
We can use the command git log --grep=Pattern/String
The Pattern need not be a single word, it can be a regular expression also.
Case 1: Display The Commits Based On The String
Example 1:
If we have to display the commits based on the string “a.txt” then we’ll use the command git log --grep=String
In our case, it’ll be git log --grep=a.txt
It’ll display all the Commits based on the string “a.txt”.
Example 2:
If we have to display the commits based on the string “added” then we’ll use the command git log --grep=String
In our case, it’ll be git log --grep=added
It’ll display all the Commits based on the string “added”.
Example 3:
We can use both the option --grep
and --oneline
together with the command git log
to filter/display the Commits based on the particular string / Patterns and give Concise Information/In Oneline about the Commit.
Syntax
git log --grep=String --oneline
In our case, it’ll be git log --grep=added --oneline
Display Commits Based On The Specific Date / Time
If we want to filter the Commit based on the specific Date / Time, then we have to use the option --since
or --after
along with the command git log. It will exclude the commit that happened on that date.
Syntax
git log --since=Date<>
Example
If we want to display/filter the commits, since/after 5th May 2021 then we can use the command git log --since=2021-5-07
We can even display the commits via days. For this, we can use the option --after
along with the command git log
. We can use the command git log --after="n days ago"
Single or double quotes are not mandatory.
Example:
If we want to display the 1 day ago then we can type the command git log --after="1 days ago"
Display Commits Based On The Author
If we want to display/display the Commits based on the Author Name, then we can use the option --author=Name/Pattern
along with the command git log
. It’ll filter the Commits based on the Author Name who has committed in the Local Repository / Remote Repository then we can use the command git log --author=Name/Pattern
Example
If we want to display all the commits made by the author "abhishekkishor" then we can use the command
git log --author=abhishekkishor
We can also use Concise Information which is the Information in One line as well. We can use both the options --author
and --oneline
together along with the command git log
.
We can use the command git log --author=Name/Pattern --oneline
Example:
If we want to display all the commits as Concise Information made by the author “abhishekkishor” then we can use the command
git log --author=abhishekkishor
To display the commit messages along with the commit information in the git log
output, we can use the --pretty=oneline
option. This will display each commit on a single line, along with its full commit hash and commit message.
git log --pretty=oneline
To display the commit history in a graphical format, you can use the --graph
option. This will display the commit history in a graph-like structure, with each commit represented by a node and its branches represented by lines.
git log --graph