Git – Reviewing Changes


Git stores all commits as snapshots. We can commit limitless. If we can to review our commits we can use log command.

Git Log

We can simple list commits without any argument tot the log command.

$ git log 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE 
 
commit 22dc9ad592d5bc21412a246791a05abd42e72793 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:16:57 2016 +0000 
 
    Version 2, Add README 
 
commit 1156333b153c7bc164e194caf49a2720e490cb61 
Author: John Doe <[email protected]> 
Date:   Fri Oct 7 09:32:07 2016 +0000 
 
    Start

As we see there are 3 commits for the whole history of the Git repository. log command provides

  • commit hash which is used as identifier and a unique value.
  • Author is the developer info like name and email address.
  • Date is the date when the commit occurs.
  • And the last line is the message added by the developer.

List Last 2 Commits

There may be a lot of commit and if we issue log command we will get a bunch of commits. We can ffilter them with -n argument by providing limit number.

$ git log -n 2 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE 
 
commit 22dc9ad592d5bc21412a246791a05abd42e72793 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:16:57 2016 +0000 
 
    Version 2, Add README

List Commits in Single Line

Listing in a more dense mode can be done with --oneline argument. This will only list commit hash and message.

$ git log --oneline -n 2 
877ab08 Version 4,Added LICENSE 
22dc9ad Version 2, Add README

List Commits Detailed

Listing detailed info about commits done with --stat command. This argument will list changing and added files.

$ git log --stat 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE 
 
 LICENSE | 1 + 
 1 file changed, 1 insertion(+) 
 
commit 22dc9ad592d5bc21412a246791a05abd42e72793 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:16:57 2016 +0000 
 
    Version 2, Add README 
 
 README.md | 1 + 
 1 file changed, 1 insertion(+)

More Details Please

To get all details about what lines are added what are diffs can be get with -p argument which means patch.

$ git log -p 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE 
 
diff --git a/LICENSE b/LICENSE 
new file mode 100644 
index 0000000..3d0f24c 
--- /dev/null 
+++ b/LICENSE 
@@ -0,0 +1 @@ 
+This is a free license 
 
commit 22dc9ad592d5bc21412a246791a05abd42e72793 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:16:57 2016 +0000 
 
    Version 2, Add README 
 
diff --git a/README.md b/README.md 
new file mode 100644 
index 0000000..4cd6e40 
--- /dev/null 
+++ b/README.md 
@@ -0,0 +1 @@ 
+#This is readme

Searching in Commits

We can look for a specific developer in the commits. We need to supply --author argument with the developer name like below.

$ git log --author="İsmail Baydan" 
$ git log -n 1 --author="John Doe"      
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE

As we see that author İsmail Baydan do not exists but John Doe working and making commits for the project.

LEARN MORE  How To Use Linux dmesg Command To Print System Logs?

Another type of search is searching messages in the commits. By providing grep argument with text we can search messages in a free text search mode.

$ git log --grep="4" 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE

Searching Between Dates / Time Ranges

Searching between dates can be done with before and after arguments. They can be used separetely or together like below.

$ git log --after="2014-7-1" 
commit 877ab08a2c122c70326bb025530e48cb673c8505 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:23:46 2016 +0000 
 
    Version 4,Added LICENSE 
 
commit 22dc9ad592d5bc21412a246791a05abd42e72793 
Author: John Doe <[email protected]> 
Date:   Sat Oct 8 04:16:57 2016 +0000 
 
    Version 2, Add README 
 
commit 1156333b153c7bc164e194caf49a2720e490cb61 
Author: John Doe <[email protected]> 
Date:   Fri Oct 7 09:32:07 2016 +0000 
 
    Start 
$ git log --before="2014-7-1"

As we have no commit before 2014-7-1 there is no result. All of our commits are after 2014-7-1.

Leave a Comment