Git: View the (detailed) commit history for a single file

Git FAQ: How do I view the detailed commit history for a file with git?

Solution: When you want the detailed git commit history for a file, this is the best git command I know:

$ git log -p --follow -- <filename>

The two important options are:

  • -p says “show all patch information”
  • --follow tells git to also show information in the event a file has been renamed

A git/log/follow example on a file

As an example, when I use that command on this file, I see several hundred lines of output that show me every line that has been added and removed from this file:

$ git log -p --follow -- divLhsSkyAd.scala.html

commit a00b98c051ec05f5f4675d004caba08037419e9d
Author: Alvin Alexander <al@alvinalexander.com>
Date:   Fri Feb 19 19:10:29 2021 -0700

    LHS ad code is now in properties file (v28)

diff --git a/src/main/twirl/divLhsSkyAd.scala.html b/src/main/twirl/divLhsSkyAd.scala.html
index 35ca388..78b08f0 100644
--- a/src/main/twirl/divLhsSkyAd.scala.html
+++ b/src/main/twirl/divLhsSkyAd.scala.html
@@ -1,30 +1,12 @@
-@(productionMode: Boolean)
+@(productionMode: Boolean, adUpperLhsContent: String)
 
 <div id="block-skyadprogramming" class="block block-block-content">
 <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><div 
-    style="width:210px;margin-left:auto;margin-right:auto;text-align:center;">
+     style="width:210px;margin-left:auto;margin-right:auto;text-align:center;">
 
 @if(productionMode) {

much more output here ...

Again, that git command shows every historical change to that file, and can easily result in hundreds or thousands of lines of detailed output for a file.

Note that you can also output this git history information to a file like this:

$ git log -p --follow -- divLhsSkyAd.scala.html > the_history.log

Show git changes that has been added but not committed

If you have modified some files in a Git repository and did a git add on them, but haven’t yet done a git commit on them, you can view those changes with this command:

git diff --cached

For example, I just ran git add . and then  git status and saw that I changed three files, but I couldn’t remember what I did to the src/main/scala/agt/Main.scala file. So I ran that command and saw this output:

$ git diff --cached

diff --git a/src/main/scala/agt/Main.scala b/src/main/scala/agt/Main.scala
index 7bba383..b4bf4b8 100755
--- a/src/main/scala/agt/Main.scala
+++ b/src/main/scala/agt/Main.scala
@@ -263,6 +263,7 @@ object main:
         wait(20)
         robot.keyRelease(keycode)
 
+    // TODO: rename to just `shift` and make it public?
     private def shiftTypeKeycode(keycode: Int): Unit =
         robot.keyPress(VK_SHIFT)
         robot.keyPress(keycode)

This shows that I added that comment just above the shiftTypeKeycode method.

That command shows every file that has been added but not committed, but each block of output begins with the filename, which is very helpful. You can also show the changes to just one file by adding the filename to the end of the command:

> git diff --cached src/main/scala/agt/Main.scala
diff --git a/src/main/scala/agt/Main.scala b/src/main/scala/agt/Main.scala
index 7bba383..b4bf4b8 100755
--- a/src/main/scala/agt/Main.scala
+++ b/src/main/scala/agt/Main.scala
@@ -263,6 +263,7 @@ object main:
         wait(20)
         robot.keyRelease(keycode)
 
+    // TODO: rename to just `shift` and make it public?
     private def shiftTypeKeycode(keycode: Int): Unit =
         robot.keyPress(VK_SHIFT)
         robot.keyPress(keycode)

As a quick recap, if you want to see the changes to one or more Git files that have been added but not committed, this git diff command shows a solution for that.

Other git history commands

There are at least two other git history/log/patch commands you can use to show the history of commits for a file:

$ git log -- divLhsSkyAd.scala.html    # one-line summary info
$ git log -p divLhsSkyAd.scala.html    # detail about each “patch”

As shown in the comment, the git log command by itself shows date, time, and the git commit message information for each commit/patch:

$ git log -- divLhsSkyAd.scala.html 

commit a00b98c051ec05f5f4675d004caba08037419e9d
Author: Alvin Alexander <al@alvinalexander.com>
Date:   Fri Feb 19 19:10:29 2021 -0700

    LHS ad code is now in properties file (v28)

commit 1152687d48d716ec8ca3871e0d2f9009302c69f7
Author: Alvin Alexander <al@alvinalexander.com>
Date:   Thu Feb 18 18:25:35 2021 -0700

    Added Kofi ad/image back to the upper-left

commit cf3cfc2ab066197ed6124754d1ee33939aa5cf5a
Author: Alvin Alexander <al@alvinalexander.com>
Date:   Fri Feb 12 18:47:14 2021 -0700

    Version 25. Tested it a little, needs more testing.

By adding the -p option to that command, git shows much more detailed for the one file you’re looking at: it shows the patches for each git commit (log entry). For my example file, adding this “patch” option results in the same output for the git log -p --follow command I showed previously, so I won’t repeat it here. In fact, if your file has never been renamed, these two commands will have the same output:

$ git log -p divLhsSkyAd.scala.html 
$ git log -p --follow -- divLhsSkyAd.scala.html

However, because the second command gives you patch details for files that have been renamed, that command is probably more useful in most situations.

In summary, if you you want to see the detailed commit history for a file, those are the best git commands I know.