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

发布于 2024-12-20 12:03:41 字数 542 浏览 0 评论 0原文

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

输出(从 gi​​t 版本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情域 2024-12-27 12:03:41

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

在此处输入图像描述

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

  1. git diff --cached
    这是索引中的内容与上次提交之间的差异。它向您显示下一次提交中将发生的更改。

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

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

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

在此处输入图像描述

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

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 --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.

  2. 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.

  3. 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 for git diff --cached because there are still changes in the index.

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.

半葬歌 2024-12-27 12:03:41

问题是:我在工作树中添加了一些文件,该文件存在于其他提交中。为什么 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 with git diff B

So尛奶瓶 2024-12-27 12:03:41

manojlds 在他的回答中写道

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

这是真的,值得投票,但它并没有让我满意,因为它既没有说“我如何比较它”,也没有给出更深入的内容其背后的原因。我仍然认为这是一个错误,所以我在 git 论坛上询问并得到了 长答案,由肯定知道的人提供。对我来说重要的部分是:

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

并且

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

manojlds wrote in his answer

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

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:

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文