git 如何将工作树与提交进行比较?

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

How to compare the working tree with a commit?

gitdiff

提问by maaartinus

I'm using

我正在使用

git diff mycommit

for comparing my working tree with mycommit, but it seems to ignore files not present in the current index. You can reproduce it as follows:

用于将我的工作树与 进行比较mycommit,但它似乎忽略了当前索引中不存在的文件。您可以按如下方式重现它:

git init
echo A > A.txt; git add .; git commit -m A; git branch A
echo B > B.txt; git add .; git commit -m B; git branch B
git reset --hard A
echo BB > B.txt
git diff B

The output (as of git version 1.7.3.3) is empty. Using --diff-filter=ACDMRTUXBshows "deleted file" which is wrong as well, since the file B.txtexists both in the working tree and in commit B. IMHO, the file should be shown as modified. Surprisingly, it works after adding the file to the index, although it's not the index what gets compared. How can I get the right diff without it?

输出(从 gi​​t 版本 1.7.3.3 开始)为空。使用--diff-filter=ACDMRTUXB显示“已删除的文件”也是错误的,因为该文件同时B.txt存在于工作树和 commit 中B。恕我直言,该文件应显示为已修改。令人惊讶的是,它在将文件添加到索引后工作,尽管它不是被比较的索引。没有它,我如何获得正确的差异?

采纳答案by maaartinus

manojlds wrote in his answer

manojlds 在他的回答中写道

Basically, diff does not take into account untracked files. That is why you see that the file is deleted, because the diff is same as git diff B A

基本上, diff 不考虑未跟踪的文件。这就是为什么你看到文件被删除的原因,因为 diff 与 git diff BA 相同

This is true and worth upvoting, but it didn't satisfy me, as it says neither "how can I compare it" not gave it the deeper reason behind. I still thought it was a bug, so I asked at the git forum and got a long answerby somebody who surely knows. The important parts for me are:

这是真实的,值得点赞,但它并没有让我满意,因为它既没有说“我怎么能比较它”也没有给出背后更深层次的原因。我仍然认为这是一个错误,所以我在 git 论坛上提问,并得到了一个肯定知道的人的长答案。对我来说重要的部分是:

The definition of paths in the working tree in these sentences is not "all files on the filesystem", or "all files on the filesystem, filtered with the ignore mechanism". It is "all files on the filesystem that are in the index" ...

这些句子中工作树中路径的定义不是“文件系统上的所有文件”,或者“文件系统上的所有文件,使用忽略机制过滤”。它是“文件系统上索引中的所有文件”......

and

...that will give a clean semantics to "git diff HEAD": what change would I be recording if I said "git commit -a" at this point?

...这将为“git diff HEAD”提供清晰的语义:如果此时我说“git commit -a”,我会记录什么变化?

回答by Abizern

A simple graphic might be of help:

一个简单的图形可能会有所帮助:

enter image description here

在此处输入图片说明

So there are three types of diff you can ask for:

因此,您可以要求三种类型的差异:

  1. git diff --cachedThis is the difference between what is in the index and the last commit. It shows you that changes that will be in the next commit.

  2. git diffThis shows the difference in between the index and the working tree. These are the changes that you have made to files since you last added them to the index. This is why I wasn't getting any results. I had no changes between the index and the working tree.

  3. git diff HEADThis shows the difference between the files in the working tree and the last commit. There is a gotcha here: if you've made changes, added them to the index, and then backed out these changes in the working tree, you'll get no results for git diff HEAD(because there is no difference) but you will get output for git diff --cachedbecause there are still changes in the index.

  1. git diff --cached这是索引中的内容和最后一次提交之间的差异。它向您显示将在下一次提交中进行的更改。

  2. git diff这显示了索引和工作树之间的差异。这些是自上次将文件添加到索引后对文件所做的更改。这就是为什么我没有得到任何结果。我在索引和工作树之间没有变化。

  3. git diff HEAD这显示了工作树中的文件和上次提交之间的差异。这里有一个问题:如果您进行了更改,将它们添加到索引中,然后在工作树中撤消这些更改,您将不会得到任何结果git diff HEAD(因为没有区别),但您会得到输出git diff --cached因为索引还是有变化的。

And if you want to compare it against previous commits:

如果您想将其与之前的提交进行比较:

enter image description here

在此处输入图片说明

This just compares against the previous commits, but you can replace HEAD~with any other reference to a commit.

这只是与之前的提交进行比较,但您可以替换HEAD~为对提交的任何其他引用。

回答by manojlds

The question is: I have added some file in my working tree, that exists in the other commit. Why is git diff <commit>not showing me the difference between the file content that exists in my working tree and the content of the file that is in the other commit.

问题是:我在我的工作树中添加了一些文件,该文件存在于另一个提交中。为什么git diff <commit>不向我展示工作树中存在的文件内容与另一个提交中的文件内容之间的区别。

This is because the new file that you added is untracked and hence, well, git doesn't track it.

这是因为您添加的新文件未跟踪,因此 git 不会跟踪它。

Try git diff HEADyou won't see that you have added B.txt in your working tree.

试试看,git diff HEAD您不会看到您在工作树中添加了 B.txt。

Basically, diff does not take into account untracked files. That is why you see that the file is deleted, because the diff is same as git diff B A

基本上, diff 不考虑未跟踪的文件。这就是为什么您看到文件被删除的原因,因为差异与git diff B A

Now, if you were to add the file, you will see the expected result, because git is tracking it now.

现在,如果您要添加文件,您将看到预期的结果,因为 git 现在正在跟踪它。

Say, you committed the file and then modify the content in the working tree:

假设您提交了文件,然后修改了工作树中的内容:

Now, git diff HEADwill show the difference between working tree and HEAD, showing the change you have done in your working tree on tracked files. Same with git diff B

现在,git diff HEAD将显示工作树和 HEAD 之间的区别,显示您在工作树中对跟踪文件所做的更改。同git diff B