git status显示已更改的文件,但git diff没有

Oli*_*r P 152 git diff

我已经看过所有类似的问题但是我已经仔细检查了一些奇怪的东西肯定会发生.

在一台服务器(Solaris with git 1.8.1)上,我克隆了git存储库,然后将.git文件夹复制到现有的实时文件中.这很完美,我可以跑

git status
Run Code Online (Sandbox Code Playgroud)

然后

git diff [filename]
Run Code Online (Sandbox Code Playgroud)

检查任何不同的文件.

在另一台服务器(Solaris with git 1.7.6)上,我做的完全一样

git diff [filename] 
Run Code Online (Sandbox Code Playgroud)

即使文件的内容肯定不同,也不显示任何内容.我还测试了添加一个新文件,然后进行编辑.同样的问题,git status将文件显示为已更改但未git diff显示任何内容.如果我下载更改的文件并在本地运行diff,那么我得到diff输出.

Adr*_*ann 71

我将文件添加到索引中:

git add file_name
Run Code Online (Sandbox Code Playgroud)

然后跑:

git diff --cached file_name
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到git diff的描述.

如果你需要撤消你的git add,那么请看这里:如何在提交之前撤消'git add'?


cmc*_*abe 56

有几个原因git status可能会显示差异,但git diff可能不会.

  • 文件的模式(权限位)已更改 - 例如,从777更改为700.

  • 换行样式从CRLF(DOS)更改为LF(UNIX)

找出所发生情况的最简单方法是运行git format-patch HEAD^并查看生成的补丁所说的内容.

  • 如果更改权限文件适用:git config core.filemode false表示忽略文件权限 (9认同)

Ale*_*rov 47

对我来说,它与文件权限有关.在我的项目中使用Mac/Linux的人似乎提交了一些具有非默认权限的文件,而我的Windows git客户端无法重现这些权限.我的解决方案是告诉git忽略文件权限:

git config core.fileMode false
Run Code Online (Sandbox Code Playgroud)

其他见解:如何让Git忽略文件模式(chmod)的变化?

  • 这解决了我在处理一堆文件时遇到的问题,尽管我认为这是文件创建/修改时间的问题。 (2认同)
  • 如果您使用在 Windows 10 上安装目录的 Docker 容器运行 VSCode,这也会有所帮助。在容器的外部,检查 git 状态,可以正确显示您尚未更改任何文件。但是如果你检查容器内的 git status,它会显示文件已更改。在容器内运行上述命令解决了我的问题。 (2认同)

Jaa*_*kko 34

我有一个问题,一些程序修改了数百行结束,git diff将所有源文件列为已更改.修复行结尾后,git status仍然将文件列为已修改.

我能够通过将所有文件添加到索引然后重置索引来解决此问题.

git add -A
git reset
Run Code Online (Sandbox Code Playgroud)

core.filemode 设置为false.

  • 我用 `git add --renormalize .` 解决了,请参阅下面我的[答案](/sf/answers/3992897051/)。 (5认同)
  • 真的很奇怪,但似乎可以工作! (4认同)

Ste*_*o M 27

正如在之前的回答中已经指出的,这种情况可能是由于行尾问题(CR/LF vs. LF)引起的。我用这个命令解决了这个问题(在 Git 版本 2.22.0 下):

git add --renormalize .
Run Code Online (Sandbox Code Playgroud)

根据手册:

       --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.
Run Code Online (Sandbox Code Playgroud)

  • 这是新的最佳答案。 (3认同)
  • 行结束问题只是一个例子;当文件提交(“clean”操作)和/或提取(“smudge”操作)时执行自动更改的其他 git 过滤器也可能是罪魁祸首;检查 git 配置文件中的“filter”部分。 (2认同)

use*_*062 17

我怀疑你的git安装或你的存储库有什么问题.

试试跑步:

GIT_TRACE=2 git <command>
Run Code Online (Sandbox Code Playgroud)

看看你是否有任何有用的东西.如果这没有帮助,只需看看出现了什么问题:

strace git <command>
Run Code Online (Sandbox Code Playgroud)

  • @towi:因为这对你来说是值得的,所以我有兴趣看看你所了解到的类似失败的原因. (6认同)
  • 在我的例子中,我将`-F`标志添加到`LESS` env变量中,如果有少于一个屏幕显示完整的信息,则该变量告诉较少退出.由于git使用less作为寻呼机,并且我有一个小的差异,所以没有显示任何内容.要么我必须在`LESS` env中添加`-X`,即使在退出次数较少之后也会在屏幕上显示内容,或者只删除`-F`.`GIT_TRACE`显示正在执行`less`,这提醒我最近更改了'LESS`变量.在@ rcwxok的答案中也有同样的理由,但想要评论一下`GIT_TRACE`是如何帮助的. (5认同)

小智 10

我有一个类似的问题:git diff会显示差异,但git diff <filename>不会.原来我设置LESS了一个包含-F(--quit-if-one-screen)的字符串.删除该标志解决了这个问题.


Sha*_*tin 9

简答

跑步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
> 
Run Code Online (Sandbox Code Playgroud)

...运行 git add 解决了不一致问题。

> git add
> git status
On branch master
nothing to commit, working directory clean
> 
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,这听起来像是“对抗症状”而不是“治愈疾病”……;) (2认同)

bin*_*yDi 9

TL;DR

Line ending issue:

  1. Change autocrlf setting to default true. I.e., checkout Windows-style line endings on Windows and commit Linux-style line endings on remote Git repository:
    git config --global core.autocrlf true
    
    Run Code Online (Sandbox Code Playgroud)
  2. On a Windows machine, change all files in the repository to Windows-style:
    unix2dos **
    
    Run Code Online (Sandbox Code Playgroud)
  3. Git add all modified files and modified files will go:
    git add .
    git status
    
    Run Code Online (Sandbox Code Playgroud)

Background

  • Platform: Windows, WSL

I occasionally run into this issue that git status shows I have files modified while git diff shows nothing. This is most likely an issue of line endings.

Root Cause

The reason I encounter this issue often is that I work on a Windows machine and interact with Git in WSL. Switching between Linux and Windows setting can easily cause this end-of-line issue. Since the line ending format used in the OS differs:

  • Windows: \r\n
  • OS X / Linux: \n

Common Practice

When you install Git on your machine, it will ask you to choose line endings setting. Usually, the common practice is to use (commit) Linux-style line endings on your remote Git repository and check out Windows-style on your Windows machine. If you use default setting, this is what Git do for you.

在此输入图像描述

This means if you have a shell script myScript.sh and Bash script myScript.cmd in your repository, the scripts both exist with Linux-style ending in your remote Git repository, and both exist with Windows-style ending on your Windows machine.

I used to check out shell script file and use dos2unix to change the script line-ending in order to run the shell script in WSL. This is why I encounter the issue. Git keeps telling me my modification of line-ending has been changed and asks whether to commit the changes.

Solution

Use the default line ending settings and if you change the line endings of some files (like use the dos2unix or dos2unix), drop the changings. If line-endings changes already exist and you would like to get rid of it, try git add them and the changes will go.


avi*_*ivr 5

陷入这个问题.我的案子类似于LESS@rcwxok发布的问题.

在我的例子中,我将PAGER环境var 设置为PAGER='less -RSF'.

然而,与之前的答案不同,我不想删除该-F选项,因为我明确地把它放在那里希望防止显示差异,less如果它比屏幕短.

为了得到理想的结果-F,我没有删除,而是添加-X:PAGER='less -RSFX'.这既解决了git diff问题,又防止了显示短差异less.

希望这有助于某人.


cfi*_*cfi 2

我对您的用例的假设:

您有一个包含文件和目录的现有目录,现在想要将其转换为从其他位置克隆的 Git 存储库,而不更改当前目录中的任何数据。

确实有两种方法。

克隆仓库 - mv .git- git reset --hard

您所做的就是此方法 - 将现有存储库克隆到空目录中,然后将该.git目录移动到目标目录中。为了没有问题地工作,这通常需要您运行

git reset --hard
Run Code Online (Sandbox Code Playgroud)

但是,这会更改当前目录中文件的状态。您可以在目录的完整副本/rsync 上尝试此操作并研究发生了什么变化。至少之后您不应该再看到git log和之间的差异status

初始化新存储库 - 指向原点

第二个不那么令人不安:cd进入你的目的地,然后启动一个新的存储库

git init
Run Code Online (Sandbox Code Playgroud)

然后你告诉这个新的存储库,它在其他地方有一个祖先:

git remote add origin original_git_repo_path
Run Code Online (Sandbox Code Playgroud)

然后平安无事

git fetch origin master
Run Code Online (Sandbox Code Playgroud)

复制数据而不更改本地文件。现在一切都应该好了。

我总是推荐第二种方法,因为这样不容易出错。