我看过所有类似的问题。然而,我又检查了一遍,确实发生了一些奇怪的事情。

在一台服务器上(Solaris with Git 1.8.1),我克隆了Git存储库,然后将. Git文件夹复制到我现有的活动文件中。这很好,我可以跑了

git status

then

git diff [filename]

检查任何不同的文件。

在另一台服务器上(Solaris with Git 1.7.6),我做的完全相同

git diff [filename]

即使文件内容完全不同,也不会显示任何内容。我还测试了添加一个新文件,提交它,然后编辑。同样的问题,git status显示文件已更改,但git diff没有显示任何内容。如果我下载更改后的文件并在本地运行diff,那么我将得到diff输出。


当前回答

有几个原因可以解释为什么git status可能会显示不同,而git diff可能不会。

文件的模式(权限位)发生了变化——例如,从777变为700。 换行风格从CRLF (DOS)到LF (UNIX)

要知道发生了什么,最简单的方法是运行git format-patch HEAD^,看看生成的补丁说了什么。

其他回答

Git diff -a将所有文件视为文本,它为我工作。

正如在前面的回答中已经提到的,这种情况可能是由于行结束问题(CR/LF vs. LF)而出现的。我用这个命令解决了这个问题(在Git 2.22.0版本下):

git add --renormalize .

根据手册:

       --renormalize
           Apply the "clean" process freshly to all tracked files to
           forcibly add them again to the index. This is useful after
           changing core.autocrlf configuration or the text attribute in
           order to correct files added with wrong CRLF/LF line endings.
           This option implies -u.

许多其他答案涉及到行尾和核心的差异。autocrlf设置。确实是这样,但我想指出另一个类似的问题:git过滤器。

如果核心。如果selflf =true,那么该行为就像一个git涂抹或清除过滤器,当文本在工作树和索引之间移动时自动处理文本。

类似地,如果你在文件最后一次提交时改变了你的git过滤器设置,它可能会导致git状态的难以捉摸的差异,而这种差异不会在git diff中显示出来。即使git添加了X;git diff——cached——X,取决于配置的过滤器的性质。

This problem happened to me when using nbstripout, a python package that implements git filters that filter out metadata before adding Jupyter notebook files to the index and before diffing them. I resolved it by reverting the filter config settings to how they were before, running git restore on the affected files, and then re-applying the changes to my filter config (because I wanted to keep the new settings long-term). For me, the settings were stored in .git/config under the filter.nbstripout and diff.ipynb sections.

我有一个类似的问题:git diff会显示差异,但git diff <filename>不会。结果是我将LESS设置为一个包含-F(——quit-if-one-screen)的字符串。移除那面旗帜就解决了问题。

我遇到了这个问题。我的情况与rcwxok发布的LESS问题类似。

在我的例子中,我将PAGER环境变量设置为PAGER='less -RSF'。

然而,与前面的答案不同,我不想删除-F选项,因为我显式地把它放在那里,希望防止在less中显示差异,如果它少于一个屏幕。

为了得到想要的结果,我没有删除-F,而是添加了-X: PAGER='less -RSFX'。这既解决了git差异的问题,此外,它防止显示短差异与较少。