如何比较工作树和提交? [英] How to compare the working tree with a commit?

查看:89
本文介绍了如何比较工作树和提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

  git diff mycommit 
mycommit ,但似乎忽略当前索引中不存在的文件。您可以如下重现它:

  git init 
echo A> A.TXT; git add。; git commit -m A; git分支A
echo B> B.txt; git add。; git commit -m B; git分支B
git reset --hard A
echo BB> B.txt
git diff B

输出(从git版本1.7.3.3开始)是空的。使用 - diff-filter = ACDMRTUXB 显示deleted file,这也是错误的,因为文件 B.txt 存在于工作树和提交 B 中。恕我直言,该文件应显示为已修改。令人惊讶的是,它在将文件添加到索引后起作用,尽管它不是索引进行比较。如何在没有它的情况下获得正确的差异?

解决方案

manojlds在他的回答中写道


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


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

lockquote

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


...这将给git diff HEAD一个干净的语义:如果我现在说git commit -a,我会记录什么变化?



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?

解决方案

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" 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 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?

这篇关于如何比较工作树和提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆