git 差异文件输出在 git 状态后发生变化
git diff-files output changes after git status
我有一个脚本 update.py
,可以下载我的 git 存储库中跟踪的文件的新版本:
$ python update.py
Doing work...
Done
$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: foo.txt
modified: bar.txt
modified: baz.txt
no changes added to commit (use "git add" and/or "git commit -a")
有时,下载的文件与 HEAD
中已有的文件相同,因此下载后工作目录是干净的:
$ python update.py
Doing work...
Done
$ git status
On branch my-branch
nothing to commit, working directory clean
但是,我发现 git diff-files
在替换文件时看起来很混乱,即使它们的内容是相同的:
$ python update.py
Doing work...
Done
$ git diff-files
:100644 100644 ffa91f655007c56f209cf15fee13c55991a76e18 0000000000000000000000000000000000000000 M foo.txt
:100644 100644 dc05558729c3c94a088aa63da3bbd8f1213b8cf3 0000000000000000000000000000000000000000 M bar.txt
:100644 100644 002cc3f53dc64b89b1b91adbb6fe61035ba9e832 0000000000000000000000000000000000000000 M baz.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$
在上面的代码片段中:
- I 运行 update.py,用从别处下载的相同副本替换
foo.txt
、bar.txt
和 baz.txt
文件。
根据 git diff man page. 中描述的原始输出格式,git diff-files
错误地报告这三个文件已在工作树中就地编辑
git status
正确报告没有任何变化。
git diff-files
、运行 在 git status
之后,现在 也 报告没有任何变化。
在 运行 宁 update.py
之后,git diff-files
将继续错误地报告更改,直到我 运行 git status
,之后它再次运行。
这是怎么回事?为什么在 none 时 git diff-files
报告更改?
如果您想知道为什么这会给我带来麻烦,请查看更多背景信息:
我有另一个脚本,update_and_commit_if_needed.py
执行以下操作:
- 运行
update.py
.
- 如果
git diff-files
returns 为零,工作树是干净的,并且 update.py
没有改变任何东西。退出。
- 否则,工作树是脏的。提交更改。
我在 update_and_commit_if_needed.py
中看到一个奇怪的失败:我会进入第三步,但随后 git commit
会抱怨有 nothing to commit, working directory clean
。在追踪该错误时,我发现了 git diff-files
的这种奇怪行为。
我在 OS X 10.11.4 (15E65) 上使用 git 版本 2.5.0。
编辑 1: 我找到了重现此行为的简单方法:
$ git diff-files
$ git status
On branch my-branch
nothing to commit, working directory clean
$ cp foo.txt ~
$ mv ~/foo.txt .
$ git diff-files
:100755 100755 20084b5d6da359748f62c259c24f2b9cc2359780 0000000000000000000000000000000000000000 M foo.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$
编辑 2: 正如评论中所建议的,我已经尝试将 core.trustctime
和 core.ignoreStat
的默认值反转。在这种情况下,这似乎并没有改变 git 的行为。
git diff-index
实际上并不检查工作树中文件的内容。相反,它使用文件的统计信息并将其与索引进行比较。事实上, diff-index
man page notes:
As with other commands of this type, git diff-index
does not actually look at the contents of the file at all. So maybe kernel/sched.c
hasn’t actually changed, and it’s just that you touched it. In either case, it’s a note that you need to git update-index
it to make the index be in sync.
如注释所示,索引的统计条目可以在 运行 git update-index --refresh
diff-files
之前更新。 man page for update-index
elaborates:
--refresh
does not calculate a new sha1 file or bring the index up-to-date for mode/content changes. But what it does do is to "re-match" the stat information of a file with the index, so that you can refresh the index for a file that hasn’t been changed but where the stat entry is out of date.
For example, you’d want to do this after doing a git read-tree
, to link up the stat index details with the proper files.
运行 update-index --refresh
之前 diff-files
消除了我描述的症状,解决了问题。
我有一个脚本 update.py
,可以下载我的 git 存储库中跟踪的文件的新版本:
$ python update.py
Doing work...
Done
$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: foo.txt
modified: bar.txt
modified: baz.txt
no changes added to commit (use "git add" and/or "git commit -a")
有时,下载的文件与 HEAD
中已有的文件相同,因此下载后工作目录是干净的:
$ python update.py
Doing work...
Done
$ git status
On branch my-branch
nothing to commit, working directory clean
但是,我发现 git diff-files
在替换文件时看起来很混乱,即使它们的内容是相同的:
$ python update.py
Doing work...
Done
$ git diff-files
:100644 100644 ffa91f655007c56f209cf15fee13c55991a76e18 0000000000000000000000000000000000000000 M foo.txt
:100644 100644 dc05558729c3c94a088aa63da3bbd8f1213b8cf3 0000000000000000000000000000000000000000 M bar.txt
:100644 100644 002cc3f53dc64b89b1b91adbb6fe61035ba9e832 0000000000000000000000000000000000000000 M baz.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$
在上面的代码片段中:
- I 运行 update.py,用从别处下载的相同副本替换
foo.txt
、bar.txt
和baz.txt
文件。
根据 git diff man page. 中描述的原始输出格式, git diff-files
错误地报告这三个文件已在工作树中就地编辑git status
正确报告没有任何变化。git diff-files
、运行 在git status
之后,现在 也 报告没有任何变化。
在 运行 宁 update.py
之后,git diff-files
将继续错误地报告更改,直到我 运行 git status
,之后它再次运行。
这是怎么回事?为什么在 none 时 git diff-files
报告更改?
如果您想知道为什么这会给我带来麻烦,请查看更多背景信息:
我有另一个脚本,update_and_commit_if_needed.py
执行以下操作:
- 运行
update.py
. - 如果
git diff-files
returns 为零,工作树是干净的,并且update.py
没有改变任何东西。退出。 - 否则,工作树是脏的。提交更改。
我在 update_and_commit_if_needed.py
中看到一个奇怪的失败:我会进入第三步,但随后 git commit
会抱怨有 nothing to commit, working directory clean
。在追踪该错误时,我发现了 git diff-files
的这种奇怪行为。
我在 OS X 10.11.4 (15E65) 上使用 git 版本 2.5.0。
编辑 1: 我找到了重现此行为的简单方法:
$ git diff-files
$ git status
On branch my-branch
nothing to commit, working directory clean
$ cp foo.txt ~
$ mv ~/foo.txt .
$ git diff-files
:100755 100755 20084b5d6da359748f62c259c24f2b9cc2359780 0000000000000000000000000000000000000000 M foo.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$
编辑 2: 正如评论中所建议的,我已经尝试将 core.trustctime
和 core.ignoreStat
的默认值反转。在这种情况下,这似乎并没有改变 git 的行为。
git diff-index
实际上并不检查工作树中文件的内容。相反,它使用文件的统计信息并将其与索引进行比较。事实上, diff-index
man page notes:
As with other commands of this type,
git diff-index
does not actually look at the contents of the file at all. So maybekernel/sched.c
hasn’t actually changed, and it’s just that you touched it. In either case, it’s a note that you need togit update-index
it to make the index be in sync.
如注释所示,索引的统计条目可以在 运行 git update-index --refresh
diff-files
之前更新。 man page for update-index
elaborates:
--refresh
does not calculate a new sha1 file or bring the index up-to-date for mode/content changes. But what it does do is to "re-match" the stat information of a file with the index, so that you can refresh the index for a file that hasn’t been changed but where the stat entry is out of date.For example, you’d want to do this after doing a
git read-tree
, to link up the stat index details with the proper files.
运行 update-index --refresh
之前 diff-files
消除了我描述的症状,解决了问题。