Git Log
The main goal of a version control system is to track changes to your code. This gives you the possibility
to navigate history commits, see who made contributions, figure out bugs, and revert changes.
Git offers the git log
command to exploit all history possibilities.
The git log
command is a utility tool to list and filter the project history and search for particular changes.
The git log
command has multiple options that can be split into two kinds:
formatting how commits are displayed and filtering which commits are displayed.
The output of the git log command is given in reverse chronological order by default. The last commit will be on top of the showed list.
Basic Git log
The git log
is a simple command and one of the most used commands of Git.
Every time you want to check the history, you can type the git log
command as follows:
$ git log
commit 4b3251c5e09390ede938a57e103afc663ee538d6 (HEAD -> new_branch)
Author: author_name <author_email@pincode.com>
Date: Mon Oct 14 23:06:14 2019 +0100
adding more text on file1
commit 6b5548fa8f6f80259cd45d211624843ebb3375ba (tag: v1.0.0, tag: shows, tag: show, master)
Author: author_name <author_email@pincode.com>
Date: Fri Oct 11 16:36:36 2019 +0100
first commit
The above command displays the most recent commits and the status of the head. Each commit includes a unique SHA-ID, the date, time, author information, and some additional details.
To scroll on the view opened by git log command, press K for moving up, J for moving down, and the spacebar for scrolling down by a full page.
If you want to exit the git log command view, press the q
button. It will quit from
the git log command view and back you to the command line.
Git log oneline
The --oneline
flag displays the output as one commit per line. It shows only
the commit ID and the first line of the commit message.
The command to shows commit history in oneline will run as follows:
$ git log --oneline
4b3251c (HEAD -> new_branch) adding more text on file1
6b5548f (tag: v1.0.0, tag: shows, tag: show, master) first commit
Git log decorate
The --decorate
flag displays all the references (branches and tags) that point to each commit
You can combine --decorate
flag with other flags like --oneline
.
The command to display all references of commits will run as follows:
$ git log --oneline --decorate
4b3251c (HEAD -> new_branch) adding more text on file1
6b5548f (tag: v1.0.0, tag: shows, tag: show, master) first commit
Git log stat
The --stat
flag displays the number of insertions and deletions to each file affected by each commit.
It also shows a summary line of the total records that have been modified.
The command will be used as follows:
$ git log --stat
commit 6b5548fa8f6f80259cd45d211624843ebb3375ba (tag: v1.0.0, tag: shows, tag: show, master)
Author: author_name <author_email@pincode.com>
Date: Fri Oct 11 16:36:36 2019 +0100
first commit
file1.txt | 1 +
file2.txt | 1 +
2 files changed, 2 insertions(+)
Git log patch
The --patch
or -p
flag displays files that have been modified and their changes.
The command to show the changes introduced by each commit will run as follows:
$ git log -p
commit 6b5548fa8f6f80259cd45d211624843ebb3375ba (tag: v1.0.0, tag: shows, tag: show, master)
Author: author_name <author_email@pincode.com>
Date: Fri Oct 11 16:36:36 2019 +0100
first commit
diff --git a/file1.txt b/file1.txt
new file mode 100644
index 0000000..3b18e51
--- /dev/null
+++ b/file1.txt
@@ -0,0 +1 @@
+hello world
diff --git a/file2.txt b/file2.txt
new file mode 100644
index 0000000..14c3187
--- /dev/null
+++ b/file2.txt
@@ -0,0 +1 @@
+Git tutorial is awesome
Git log graph
The --graph
flag will draw an ASCII representing the commit history's branch structure.
It's common to use --graph
option with --oneline
and --decorate
options to make it easy and beautiful
to display a nice representation of the commit history.
The command to display a nice commit history will run as follows:
$ git log --graph --oneline --decorate
To make it very easy to display a nice commit history, you can create an alias to make it very easy to type the last command ever time.
$ git config --global alias.hist 'log --graph --oneline --decorate'
The above command creates an alias hist
, which will show a nice presentation of the commit history.
Custom formatting
The pretty=format:
option lets you display each commit the way you want using printf-style placeholders.
For example, the %cn, %H placeholders in the command fellow are replaced with the name of the person who made the commit, and the commit hash.
$ git log --pretty=format:"%H commited by %cn"
4b3251c5e09390ede938a57e103afc663ee538d6 commited by author_name
6b5548fa8f6f80259cd45d211624843ebb3375ba commited by author_name
Filtering the commit history
Git log command offers the possibility to filter the output according to your needs. Many filters can be applied by amount, by date, by author, by content.
Let's see each of these filters in detail.
By amount
The simplest filtering option for the git log command is limiting the number of commits displayed.
You can limit git log output by including the -<n>
option. The following example will show only the 4 most recent commits.
$ git log -4
By date
You can use the --after
and --before
flags to filter the log's output by date. Both options accept different
date formats as a parameter.
The following command will list all the commits after "2012-02-26"
$ git log --after="2019-02-26"
You can also pass in relative references like "12 days ago", "1 week ago" and "yesterday":
$ git log --after="12 days ago"
You can also search for commits between two dates. For example, to show all the commits between 2012-09-16 and 2012-10-12, you would type the following command:
$ git log --after="2019-09-16" --before="2019-10-12"
By author
If you want to search for commits created by a particular user, apply the --author
flag.
It will filter the commits by author name.
The command git log --author="author_name"
can take a regular expression and returns
the list of commits created by the given user that match the given pattern.
The command to filter by the exact name author run as follows:
$ git log --author="author_name"
By commit message
To filter by commit message, use the --grep
flag. It works similar to the --author
flag,
but it filters by the commit message instead of the author.
For example, you want to search for commits that change the sorting algorithm you and your team are working with.
$ git log --grep ="sort"
The command above displays all the commits that contain the word "sort" in its commit message.
By content
You can also search for commits that add or remove a particular line of source code.
If you want to search for a string, use the -S""
option, and for a regular expression, use the -G""
option.
For example, if you want to know on which commits the string "hello" was added, you would use the following command :
$ git log -S"hello"