Search code examples
gitgit-log

Git log -p -- <file> shows a commit with no patch


I asked git to show me all the commits that modified a certain file, and it produced what I asked, except some commits show no modification to said file (and so instead only the commit hash/author/message etc... appears). It seems that most of these commits are merges. What could be the meaning of this ?

Command : git log -p -- <path/to/file>

commit <hashM>
Merge: <hash1> <hash2>
Author: Name <Email>
Date:   <Date>

    Merged with develop.


commit <hashC>
Author: <Name> <Email>
Date:   <Date>

   Modified file to add new header.

diff --git a/<path/to/file.cpp> b/<path/to/file.cpp>
index ece9ea6fc..370c23a50 100644
--- a/<path/to/file.cpp>
+++ b/<path/to/file.cpp>
@@ -1,6 +1,12 @@
 #include "Header.h"

+#include "NewHeader.h"
 #include "StandardHeader.h"
 #include "OtherHeader.h"

Here commit hashM is weird to me, because it appears as an output of the command, yet shows no modification to the file. hashC on the other hand is not strange : it has modifications to the file.


Solution

  • By default git log does not show diffs on merges, it thinks you're trying to find the origin of some change, not the (possibly quite numerous) merges that just propagated it.

    But wanting those diffs is common enough there's a flag for it, a short option: git log -m will do diffs against followed merge parents, so for instance if you want to temporarily treat all merges as squash merges you can do git log -m --first-parent.