git status 显示更改的文件,但 git diff 不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14564946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:21:55  来源:igfitidea点击:

git status shows changed files but git diff doesn't

gitdiff

提问by Oliver P

I've had a look at all similar questions however I've double checked and something strange is definitely happening.

我看过所有类似的问题,但是我已经仔细检查过,肯定会发生一些奇怪的事情。

On one server (Solaris with git 1.8.1) I cloned the git repository then copied the .git folder into my existing live files. This worked perfectly, I could run

在一台服务器(带有 git 1.8.1 的 Solaris)上,我克隆了 git 存储库,然后将 .git 文件夹复制到我现有的实时文件中。这工作得很好,我可以运行

git status

then

然后

git diff [filename]

to check any files that were different.

检查任何不同的文件。

On another server (Solaris with git 1.7.6) I'm doing exactly the same however

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

git diff [filename] 

shows nothing, even if the contents of the file is definitely different. I have also tested adding a new file, committing it then editing. Same issue, git statusshows the file as changed but git diffshows nothing. If I download the changed file and run a diff locally then I get diff output.

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

回答by atw

I added the file to the index:

我将文件添加到索引中

git add file_name

and then ran:

然后跑:

git diff --cached file_name

You can see the description of git diff here.

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

If you need to undo your git add, then please see here: How to undo 'git add' before commit?

如果您需要撤消 git add,请参阅此处:如何在提交前撤消“git add”?

回答by cmccabe

There are a few reasons why git statusmight show a difference but git diffmight not.

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

  • The mode (permission bits) of the file changed-- for example, from 777 to 700.

  • The line feed style changed from CRLF (DOS) to LF (UNIX)

  • 文件的模式(权限位)发生了变化——例如,从 777 更改为 700。

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

The easiest way to find out what happened is to run git format-patch HEAD^and see what the generated patch says.

找出发生了什么的最简单方法是运行git format-patch HEAD^并查看生成的补丁内容。

回答by Alex Abdugafarov

For me, it had something to do with file permissions. Someone with Mac/Linux on my project seems to commit some files with non-default permissions which my Windows git client failed to reproduce. Solution for me was to tell git to ignore file permissions:

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

git config core.fileMode false

Other insight: How do I make Git ignore file mode (chmod) changes?

其他见解:如何让 Git 忽略文件模式 (chmod) 更改?

回答by Jaakko

I had an issue where hundreds of line endings were modified by some program and git diff listed all source files as changed. After fixing the line endings, git status still listed the files as modified.

我有一个问题,数百行结尾被某个程序修改,git diff 将所有源文件列为已更改。修复行尾后, git status 仍将文件列为已修改。

I was able to fix this problem by adding all files to index and then resetting the index.

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

git add -A
git reset

core.filemodewas set to false.

core.filemode被设置为假。

回答by user1338062

I suspect there is something wrong either with your git installation or your repository.

我怀疑您的 git 安装或存储库有问题。

Try running:

尝试运行:

GIT_TRACE=2 git <command>

See if you get anything useful. If that doesn't help, just strace and see what's going wrong:

看看有没有什么有用的。如果这没有帮助,只需跟踪并查看发生了什么问题:

strace git <command>

回答by rcwxok

I had a similar problem: git diffwould show differences, but git diff <filename>would not. It turned out that I set LESSto a string including -F(--quit-if-one-screen). Removing that flag solved the problem.

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

回答by Stefano M

As already noted above, this situation may arise due to line-ending problems (CRLF vs LF). I solved this problem (under git version 2.22.0) with this command:

正如已经指出的上面,这种情况可能由于行结束的问题(CRLF VS LF)。我用这个命令解决了这个问题(在 git 版本 2.22.0 下):

git add --renormalize .

According to the manual:

根据手册:

       --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.

回答by avivr

Ran into this problem. My case was similar to LESSissue posted by @rcwxok.

遇到了这个问题。我的案例类似于LESS@rcwxok 发布的问题。

In my case, I set the PAGERenvironment var to PAGER='less -RSF'.

就我而言,我将PAGER环境变量设置为PAGER='less -RSF'.

However, unlike the previous answers, I didn't want to remove the -Foption because I explicitly put it there hoping to prevent showing the diff in lessif it's shorter than a screenful.

但是,与之前的答案不同,我不想删除该-F选项,因为我明确地将它放在那里,希望在less它比屏幕短时防止显示差异。

To get the desired result, instead of removing -F, I added -X: PAGER='less -RSFX'. This both solved the git diffissue and in addition it prevents showing short diffs with less.

为了获得所需的结果-F,我没有删除,而是添加了-X: PAGER='less -RSFX'。这既解决了这个git diff问题,而且还防止了与less.

Hope this helps someone.

希望这可以帮助某人。

回答by Shaun Luttin

Short Answer

简答

Running git addsometimes helps.

跑步git add有时会有所帮助。

Example

例子

Git status is showing changed files and git diff is showing nothing...

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
> 

...running git add resolves the inconsistency.

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

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

回答by Alexandr Lazarev

I've just run in a similar issue. git diff fileshowed nothing because I added file to the git index with some part of it's name in uppercase: GeoJSONContainer.js. Afterwards, I've renamed it to GeoJsonContainer.jsand changes stopped being tracked. git diff GeoJsonContainer.jswas showing nothing. Had to remove file from index with a force flag, and add the file again:

我刚刚遇到了类似的问题。git diff file什么也没显示,因为我将文件添加到 git 索引,其中部分名称大写: GeoJSONContainer.js. 之后,我将其重命名为GeoJsonContainer.js并停止跟踪更改。git diff GeoJsonContainer.js什么也没显示。必须使用强制标志从索引中删除文件,然后再次添加文件:

git rm -f GeoJSONContainer.js 
git add GeoJSONContainer.js