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

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

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 -p option says “show all patch information,” and the --follow option tells git to also show information in the event a file has been renamed.

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.

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.