如何将工作树与提交进行比较?
我
git diff mycommit
用于将我的工作树与 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
输出(从 git 版本 1.7.3.3 开始)为空。使用 --diff-filter=ACDMRTUXB
显示“已删除的文件”,这也是错误的,因为文件 B.txt
既存在于工作树中又存在于提交中 B。恕我直言,该文件应显示为已修改。令人惊讶的是,它在将文件添加到索引后起作用,尽管比较的不是索引。没有它我怎样才能得到正确的差异?
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:
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=ACDMRTUXB
shows "deleted file" which is wrong as well, since the file B.txt
exists 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的图形可能会有所帮助:
因此,您可以要求三种类型的差异:
git diff --cached
这是索引中的内容与上次提交之间的差异。它向您显示下一次提交中将发生的更改。
git diff
这显示了索引和工作树之间的差异。这些是自您上次将文件添加到索引以来对文件所做的更改。这就是为什么我没有得到任何结果。我在索引和工作树之间没有任何更改。
git diff HEAD
这显示了工作树中的文件与上次提交之间的差异。这里有一个问题:如果您进行了更改,将它们添加到索引中,然后在工作树中撤销这些更改,您将不会得到 git diff HEAD 的结果(因为有没有区别),但您将获得 git diff --cached 的输出,因为索引中仍然存在更改。
如果您想将其与以前的提交进行比较:
这只是与以前的提交进行比较,但是您可以将
HEAD~
替换为对提交的任何其他引用。A simple graphic might be of help:
So there are three types of diff you can ask for:
git diff --cached
This 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.
git diff
This 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.
git diff HEAD
This 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 forgit diff --cached
because there are still changes in the index.And if you want to compare it against previous commits:
This just compares against the previous commits, but you can replace
HEAD~
with any other reference to a commit.问题是:我在工作树中添加了一些文件,该文件存在于其他提交中。为什么 git diff没有向我显示工作树中存在的文件内容与其他提交中的文件内容之间的差异。
这是因为您添加的新文件未跟踪,因此 git 不会跟踪它。
尝试 git diff HEAD ,您将不会看到您已在工作树中添加了 B.txt 。
基本上,diff 不考虑未跟踪的文件。这就是为什么您看到该文件被删除,因为 diff 与 git diff B A 相同
现在,如果您要添加该文件,您将看到预期的结果,因为 git 现在正在跟踪它。
假设您提交了文件,然后修改了工作树中的内容:
现在,
git diff HEAD
将显示工作树和 HEAD 之间的差异,显示您所做的更改在跟踪文件的工作树中完成。与 git diff B 相同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.This is because the new file that you added is untracked and hence, well, git doesn't track it.
Try
git diff HEAD
you won't see that you have added B.txt in your working tree.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
Now, if you were to add the file, you will see the expected result, because git is tracking it now.
Say, you committed the file and then modify the content in the working tree:
Now,
git diff HEAD
will show the difference between working tree and HEAD, showing the change you have done in your working tree on tracked files. Same withgit diff B
manojlds 在他的回答中写道
这是真的,值得投票,但它并没有让我满意,因为它既没有说“我如何比较它”,也没有给出更深入的内容其背后的原因。我仍然认为这是一个错误,所以我在 git 论坛上询问并得到了 长答案,由肯定知道的人提供。对我来说重要的部分是:
并且
manojlds wrote in his answer
This is true and worth upvoting, but it didn't satisfy me, as it says neither "how can I compare it" nor gave the deeper reason behind it. I still thought it was a bug, so I asked at the git forum and got a long answer by somebody who surely knows. The important parts for me are:
and