git:为什么 git diff 没有显示任何差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4734844/
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 09:53:32  来源:igfitidea点击:

git: Why doesn't git diff show any differences?

gitgit-diffgit-status

提问by Noam

If I run 'git status' on my repo it gives:

如果我在我的 repo 上运行“git status”,它会给出:

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified: myfile

However, if I do a 'git diff myfile' it shows no differences. Is this because I have made a change and removed it so it is back to the original?

但是,如果我执行 'git diff myfile',它不会显示任何差异。这是因为我做了更改并删除了它以便恢复到原始状态吗?

Should I run 'git checkout myfile' to clear it?

我应该运行 'git checkout myfile' 来清除它吗?

回答by Marcus Borkenhagen

Your file is already staged to be committed. You can show it's diff using the --cachedoption of git.

您的文件已准备提交。您可以使用--cachedgit 选项显示它的差异。

git diff --cached myfile

To unstage it, just do what git status suggests in it's output ;)

要取消暂存,只需执行 git status 在其输出中建议的操作;)

You can check The Git IndexFor more info.

您可以查看Git 索引以获取更多信息。

回答by Liyan Chang

I have a preference for the --stagedalias, mostly because I find that --stagedactually means what I want to do, i.e. show me the staged differences.

我更喜欢--staged别名,主要是因为我发现这--staged实际上意味着我想要做的事情,即向我展示阶段性差异。

git diff --staged

The accepted answer is correct and I have no qualms with it. Just personally think that --cachedfeels like git diff is caching the answer and showing me pre-calculated results or something.

接受的答案是正确的,我对此没有任何疑虑。只是个人认为--cached感觉就像 git diff 正在缓存答案并向我展示预先计算的结果或其他东西。

I also like git diff HEADbecause it's a more general. It puts together two concepts that most folks know, that is:

我也喜欢,git diff HEAD因为它更通用。它将大多数人都知道的两个概念放在一起,即:

  1. git diff <commit>allows you to see the difference between your current position and a previous commit.

  2. HEAD(or headbecause holding shift is annoying) is a reference to the tip of your branch. (For those of you who are counting keystrokes, @is an alias for HEAD.)

  1. git diff <commit>允许您查看当前位置和先前提交之间的差异。

  2. HEAD(或者head因为按住 shift 很烦人)是对分支尖端的引用。(对于那些计算击键次数的人,@是 的别名HEAD。)

Combined, these two generally useful concepts result in:

结合起来,这两个通常有用的概念导致:

git diff head
git diff @

回答by Goyal Vicky

For Staging Area vs Repository comparison use

对于暂存区与存储库比较使用

$git diff --staged

For Working vs Repository comparison use

对于工作与存储库比较使用

$ git diff 

but if a file is changed and added to staging area ($ git add fileName) and we try to see the difference with ( $ git diff). It will not return any difference since the file is in staging area and it will not be compared with the repository.

但是如果文件被更改并添加到暂存区 ( $ git add fileName) 并且我们尝试查看与 ( $ git diff)的区别。它不会返回任何差异,因为文件在暂存区并且不会与存储库进行比较。