简体   繁体   English

为什么git diff不显示有关特定提交和文件的信息?

[英]Why git diff doesn't show information on a specific commit and file?

I made changes to file source.json by removing a line from it. 我通过从文件中删除一行来更改了文件source.json I committed the change (other files were changed as well). 我提交了更改(其他文件也更改了)。 I then made another commit. 然后,我再次提交。

Now I want to see the changes I made to source.json using git diff . 现在我想看看我使用git diffsource.json所做的更改。

What I do is: 我要做的是:

git diff HEAD^^ -- path/to/source.json

I don't get any output though (the command's exit status is 0). 我没有任何输出(命令的退出状态为0)。 If I enter git diff HEAD^^ I do see the change source.json along with all other changed files. 如果输入git diff HEAD^^我会看到更改source.json以及所有其他更改的文件。

This is the output of git diff-tree --no-commit-id --name-only -r HEAD^ : 这是git diff-tree --no-commit-id --name-only -r HEAD^

path/to/AboutUs.js
path/to/Contact.js
path/to/source.json

This is the output of git diff HEAD^^ : 这是git diff HEAD^^的输出:

diff --git a/path/to/source.json b/path/to/source.json
index 3ba32e950..d86eb9c25 100644
--- a/path/to/source.json
+++ b/path/to/source.json
@@ -565,7 +565,6 @@
-  "someContent": "someContent",
   "someContent2": "someContent2",

Is there anything I'm missing? 有什么我想念的吗?

While git diff does allow pathspecs to limit its output to files matching the pathspec, what is sometimes a bit surprising is that these pathspecs are relative to the current position within the work-tree. 尽管git diff 确实允许pathspec将其输出限制为与pathspec匹配的文件,但有时有些令人惊讶的是,这些pathspec相对于工作树中的当前位置

In other words, if: 换句话说,如果:

 git diff-tree --no-commit-id --name-only -r HEAD^ 

produces: 生产:

 path/to/AboutUs.js path/to/Contact.js path/to/source.json 

and you are at the top level of your work-tree but then do: 并且您位于工作树的顶层 ,但是请执行以下操作:

cd path/to

you will need to use: 您将需要使用:

git diff HEAD^^ -- source.json

as the last argument effectively means ./source.json . 作为最后一个参数有效地意味着./source.json

Alternatively, after: 或者,之后:

git rev-parse --show-cdup

prints ../.. you can do: 打印../..您可以执行以下操作:

git -C ../.. diff HEAD^^ -- path/to/source.json

as the -C moves that one git diff command temporarily up two levels. -C将一个git diff命令暂时上移两个级别时。 (I've used this general pattern in scripts and aliases, eg, (我在脚本和别名中使用了这种通用模式,例如,

git -C "$(git rev-parse --show-cdup)" ls-files --other --exclude-standard

in a bit of shell code to detect untracked files.) 用一些shell代码来检测未跟踪的文件。)


Git is not completely consistent here. Git在这里并不完全一致。 If you wish to view the version of source.json that is in commit HEAD^^ while in this subdirectory, you must run: 如果您希望在此子目录中查看提交HEAD^^source.json版本,则必须运行:

git show HEAD^^:./source.json

as: 如:

git show HEAD^^:filename

refers to the file named filename in the top level of the repository—even though you're still in path/to . 指的是存储库顶层中名为filename的文件-即使您仍在path/to The trick for git show in particular is that the file name supplied after the commit-specifier is not a pathspec argument. 特别是git show的窍门是,在commit-specifier之后提供的文件名不是 pathspec参数。 But other Git commands that do take pathspecs may in effect assume :(toplevel) . 但是其他确实采用pathspecs的Git命令实际上可能会采用:(toplevel)

For more about pathspecs, see the gitglossary . 有关pathspec的更多信息,请参见gitglossary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM