为什么`git diff`在`git add`后报告没有文件更改

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

why `git diff` reports no file change after `git add`

gitgit-diffgit-add

提问by eridal

?Why is that git diffthinks there are no changes

?为什么git diff认为没有变化

..even if git statusreports them as modified?

..即使git status将它们报告为已修改

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   file-added
    modified:   file-with-changes   << it knows there are changes

but in order to see the difference, I need to explicitly add the last reversion hash..

但为了看到差异,我需要明确添加最后一个还原哈希..

$ git diff
  (nothing)

$ git diff rev-hash
diff --git a/file-with-changes b/file-with-changes
index d251979..a5fff1c 100644
--- a/file-with-changes
+++ b/file-with-changes
.
..

回答by Bhaskar

Please try git diff --stagedcommand.

请尝试git diff --staged命令。

More options you can use.

您可以使用更多选项。

git diff

git diff

shows changes between index/staging and working files. In your case, git addput the file-with-changesto staging area. Hence, no difference between staging and working files.

显示索引/暂存文件和工作文件之间的变化。在您的情况下,git add将 放在file-with-changes暂存区。因此,暂存文件和工作文件之间没有区别。

git diff --staged

git diff --staged

shows changes between HEAD and index/staging. git diff --cachedalso does the same thing. stagedand cachedcan be used interchangeably.

显示 HEAD 和索引/暂存之间的变化。git diff --cached也做同样的事情。staged并且cached可以互换使用。

git diff HEAD

git diff HEAD

shows changes between HEAD and working files

显示 HEAD 和工作文件之间的变化

git diff $commit $commit

git diff $commit $commit

shows changes between 2 commits

显示 2 次提交之间的变化

git diff origin

git diff origin

shows diff between HEAD & remote/origin

显示 HEAD 和远程/原点之间的差异

回答by Carl Norum

git diffdiffs against the index, not against your HEADrevision. By running git add, you've put the changes in your index, so of course there are no differences! Use

git diff与索引不同,而不是与您的HEAD修订版不同。通过运行git add,您已将更改放入索引中,因此当然没有区别!用

  • git diff HEADto see the differences between your tree state and the HEADrevision, or
  • git diff --cachedto see the differences between your index and the HEADrevision.
  • git diff HEAD查看您的树状态和HEAD修订版之间的差异,或
  • git diff --cached查看您的索引和HEAD修订版之间的差异。

回答by Rishiaar

Ran into the exact same problem.

遇到了完全相同的问题。

  • Add the new file that you created using git add filename1.c
  • Make another change in some other filename2.c that was already a part of the repository tracking system.
  • Do a git diff and you will only see the change to filename2.c show up. Changes to filename1.c will not show up.
  • However if you do a git status you will see the changes in both filename1.c and filename2.c show up.
  • Do a git commit -a -m "Changes to filename1.c and filename2.c blah blah"
  • Do a git push
  • 添加您使用 git add filename1.c 创建的新文件
  • 对其他一些已经是存储库跟踪系统一部分的 filename2.c 进行另一次更改。
  • 执行 git diff,您将只会看到对 filename2.c 的更改。不会显示对 filename1.c 的更改。
  • 但是,如果您执行 git status,您将看到 filename1.c 和 filename2.c 中的更改。
  • 做一个 git commit -a -m "Changes to filename1.c and filename2.c blah blah"
  • 做一个 git push

You will see that filename1.c got committed.

您将看到 filename1.c 已提交。

回答by Dave Zych

Because git diffby default checks differences between the staging area and your working copy. When you git add, your staging area matches your working copy and therefore diff reports no changes.

因为git diff默认情况下会检查暂存区和您的工作副本之间的差异。当您 时git add,您的暂存区与您的工作副本相匹配,因此 diff 不会报告任何更改。

Adding the --cachedflag tells diff to diff against HEAD.

添加--cached标志告诉 diff 与 diff 相对HEAD