Using the Git Log Command

The git log command allows you to view the commit history of a repository. This is especially useful if you want to look back over previous changes, whether they’ve been carried out by you or other developers.

When running the command, you will probably get something like the below.

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: John Doe <johndoe@example.com>
Date:   Mon Mar 17 21:52:11 2016 +0100

    Changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 16:40:33 2016 +0100

    Removed unnecessary text

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 10:31:28 2016 +0100

    Initial commit

Without passing in any arguments to git log, the commits are listed in reverse chronological order.

You can add a ‘-<n>’ argument where ‘n’ is the number of previous commit entries. This is useful if you only need to view a certain number of previous commits.

$ git log -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: John Doe <johndoe@example.com>
Date:   Mon Mar 17 21:52:11 2016 +0100

    Changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 16:40:33 2016 +0100

    Removed unnecessary text

As well as this, you can format the log entries to just one line by adding the ‘–oneline’ argument.

$ git log --oneline
cd7f75f Changed the version number
ae85858 Removed unnecessary text
0acefc1 Initial commit

Note here that the commit’s initial hash is used and not the longer SHA-1 checksum that was seen in the above examples.

Using the ‘–decorate’ argument makes git log display all of the branch, tag references and more that point to each commit.

$ git log --decorate --oneline
0e25143 (HEAD, master) Merge branch 'feature'
ad8621a (feature) Fix a bug in the feature branch
16b36c6 Initial commit

To view the commit history that made changes to a specific file, use the ‘–follow’ flag followed by the filename you’d like to track.

$ git log --follow index.html
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 10:31:28 2016 +0100

    Initial commit

View the diff with each commit by passing in either the ‘–stat’ or ‘-p’ arguments.

‘–stat’ displays the number of insertions and deletions to each file altered by each commit.

git log --follow --stat index.html
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 10:31:28 2016 +0100

    Initial commit

 index.html | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Specifying the ‘-p’ option shows the actual changes introduced by each commit.

git log --follow --stat index.php
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: John Doe <johndoe@example.com>
Date:   Sat Mar 15 10:31:28 2016 +0100

    Initial commit

diff --git a/index.php b/index.php
new file mode 100644
index 0000000..dd3d554
--- /dev/null
+++ b/index.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Front to the WordPress application. This file doesn't do anything, but loads
+ * wp-blog-header.php which does and tells WordPress to load the theme.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Tells WordPress to load the WordPress theme and output it.
+ *

The git shortlog command allows you to view each commit by the author with the commit message. The summary shown is in a format suitable for release announcements.

$ git shortlog
John (2):
      Initial commit
      Removed unnecessary text
      Changed version number

Jane (1):
      Added CSS fixes

There are a lot of other git log features to learn, so be sure to check out the official documentation to help you learn more about the power of Git.