Search code examples
gitgit-diff

Why Git diff of 2 versions of a single file is empty?


I'm trying to find the git diff between 2 versions of a file. I do not want to use the SHA.

I have tried a dozen variations, the diff comes up empty. I am not looking for a diff against the working or staged files. I edited the path on the output so the line would fit without wrapping.

I am using posh-git powershell.

Here is proof the file exists:

ls src/G01/P001_MultiplesOf3And5.java

Directory: U:\dev\workspace\ProjectEuler\src\G01


Mode                LastWriteTime     Length Name  
----                -------------     ------ ----  
-a---         5/24/2015   7:38 AM       1612 P001_MultiplesOf3And5.java

Here is what I am trying to do:

U: git diff HEAD~1..HEAD src/G01/P001_MultiplesOf3And5.java

U: git diff HEAD~1..HEAD -- src/G01/P001_MultiplesOf3And5.java

U: 

I read that this should work: git diff $start_commit..$end_commit -- path/to/file

You can see the diff comes up empty. Any ideas?


Solution

  • You need to find the last two commits that did include a given file.
    Those might not be HEAD or HEAD~. If yourFile is unchanged in HEAD and HEAD~, then git diff returns nothing.

    The full command would be:

     git diff $(git log -n2 --pretty=format:%H -- yourFile | awk '{print $1}' | tac) -- yourFile
    

    With:

    • git log -n2 --pretty=format:%H -- yourFile: will get the last 2 commits
    • awk '{print $1}': make sure they are printed one per line
    • tac: reverse the order (oldest commit, then newest)
    • git diff $(...) -- yourFile: does a diff of yourFile using relevant commit SHA1.