git 显示带有重命名检测的文件(相当于 git log --follow)

git show file with rename detection (equivalent to git log --follow)

我想在重命名完成之前以提交状态查看过去重命名的文件的内容(当然有自动检测)。

通过 git log --follow foo_renamed.txtgit show master~20 foo_original_name.txt 我可以确认 git 能够跟踪文件重命名并且文件存在于 master~20 中。然而,

git show --follow master~20:foo_renamed.txt

git show -M master~20:foo_renamed.txt

都失败了

fatal: Path 'foo_renamed.txt' exists on disk, but not in 'master~20'.

这实际上是有道理的,因为对象规范是 <rev>:<path> blob,但 git show 没有 -- /file/path 选项。还有别的办法吗?谢谢。

不幸的是,没有。只有 git log --follow 实现了特殊情况 hack 来跟踪这些检测到的重命名(它们必须被检测到,一次一个提交,同时通过历史回溯),并且它使用不太适合的代码来实现完成这项任务。

如果您想在 Git 上工作,应该可以修改代码以使其更灵活,并在 git show 中添加一个选项 git show作为目标提交后代的提交的 ID,但 拥有该文件。 Git 然后必须像 git log 那样遍历修订,进行重命名检测(但使用这个更灵活的代码),以便当提交图遍历到达要显示的提交时,Git 将能够使用较早的名称。

调用可能类似于:

git show --follow[=<start>] commit -- path

其中 <start> 默认为 HEAD。不过,这是一件很重要的事情。

(此外,git show 应该 拒绝 --follow 现在;它允许它的事实是一个小错误。)

我相信如果 torek 说 git show 现在不能这样做,那就是目前的情况。

我现在的解决方法是:

git show master~42:`git log --follow master~42..master \
--name-only --oneline -- path/to/file | tail -n1`