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

在一台服务器上(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^,看看生成的补丁说了什么。

其他回答

正如在前面的回答中已经提到的,这种情况可能是由于行结束问题(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.

简短的回答

运行git add有时会有所帮助。

例子

Git状态显示已更改的文件和Git diff显示什么都没有…

> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")
> git diff
> 

运行git add可以解决不一致的问题。

> git add
> git status
On branch master
nothing to commit, working directory clean
> 

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

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

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

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

我又偶然发现了这个问题。但这一次发生的原因不同。 我将文件复制到存储库中以覆盖以前的版本。现在我可以看到文件被修改了,但是diff没有返回diff。

例如,我有一个主页。xaml文件。 在文件资源管理器中,我粘贴了一个新的主页。Xaml文件超过了我当前存储库中的一个。 我在另一台机器上做了这项工作,只是把文件粘贴到这里。

文件显示已修改,但当我运行git diff时,它不会显示更改。

这可能是因为文件中的fileinfo已经更改,Git知道它实际上不是同一个文件。有趣。

您可以看到,当我在文件上运行diff时,它什么也不显示,只是返回提示符。

许多其他答案涉及到行尾和核心的差异。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.