为什么`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
why `git diff` reports no file change after `git add`
提问by eridal
?Why is that git diff
thinks there are no changes
?为什么git diff
认为没有变化
..even if git status
reports 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 --staged
command.
请尝试git diff --staged
命令。
More options you can use.
您可以使用更多选项。
git diff
git diff
shows changes between index/staging and working files. In your case, git add
put the file-with-changes
to 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 --cached
also does the same thing. staged
and cached
can 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 diff
diffs against the index, not against your HEAD
revision. 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 HEAD
to see the differences between your tree state and theHEAD
revision, orgit diff --cached
to see the differences between your index and theHEAD
revision.
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 diff
by 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 --cached
flag tells diff to diff against HEAD
.
添加--cached
标志告诉 diff 与 diff 相对HEAD
。