在 Git 中查看已删除的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1395445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Viewing a Deleted File in Git
提问by Colin Ramsay
I've deleted a file with Git and then committed, so the file is no longer in my working copy. I want to look at the contents of that file, but not actually restore it. How can I do this?
我用 Git 删除了一个文件然后提交,所以该文件不再在我的工作副本中。我想查看该文件的内容,但实际上并不还原它。我怎样才能做到这一点?
回答by CB Bailey
git show HEAD^:path/to/file
You can use an explicit commit identifier or HEAD~n
to see older versions or if there has been more than one commit since you deleted it.
您可以使用显式提交标识符或HEAD~n
查看旧版本,或者自删除以来是否有多个提交。
回答by Louis
If this is a file you've deleted a while back and don't want to hunt for a revision, you can use (the file is named foo
in this example; you can use a full path):
如果这是一个您已删除一段时间的文件,并且不想寻找修订版,则可以使用(foo
在本例中为该文件命名;您可以使用完整路径):
git show $(git rev-list --max-count=1 --all -- foo)^:foo
The rev-list
invocation looks for all the revisions of foo
but only lists one. Since rev-list
lists in reverse chronological order, then what it lists is the last revision that changed foo
, which would be the commit that deleted foo
. (This is based on the assumption that git does not allow a deleted file to be changed and yet remaindeleted.) You cannot just use the revision that rev-list
returns as-isbecause foo
no longer exists there. You have to ask for the one just before it which contains the last revision of the file, hence the ^
in git show
.
该rev-list
调用会查找 的所有修订版,foo
但仅列出一个。由于rev-list
按时间倒序列出,那么它列出的是最后更改的修订版foo
,这将是删除的提交foo
。(这是基于这样的假设,即 git 不允许更改已删除的文件,但仍保持删除状态。)您不能只使用按原样rev-list
返回的修订版,因为那里不再存在。您必须要求在它之前包含文件的最后一个修订版的那个,因此in 。foo
^
git show
回答by pdeschen
Since you might not recall the exact path, you can instead get the sha1 from git log then you can simply issue
由于您可能不记得确切的路径,您可以改为从 git log 获取 sha1 然后您可以简单地发出
git cat-file -p <sha1>