简体   繁体   中英

Viewing a Deleted File in Git

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 show HEAD^:path/to/file

您可以使用显式提交标识符或HEAD~n来查看旧版本,或者如果您删除它以后已经有多个提交。

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):

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 remain deleted.) You cannot just use the revision that rev-list returns as-is because 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 .

由于你可能不记得确切的路径,你可以从git log获取sha1然后你可以简单地发出

 git cat-file -p <sha1>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM