通常,您可以这样做:
$ echo "Stanley, you must beware of the Drive bee" > file-a
$ echo "What's a Drive bee?" > file-b
$ git init .
$ git add file-b
$ git commit file-b -m "We don't know, but whatever error you make with it could be fatal."
$ git reset --hard HEAD
$ ls
file-a file-b
Run Code Online (Sandbox Code Playgroud)
我觉得我做得非常糟糕:
$ echo "What are you doing, you darn ?" > file-a
$ echo "Can't you see I'm trying to drive?" > file-
$ git init .
$ git add -A
$ git commit file- -m "Oh, my God! [It's] the Drive !"
$ git reset --hard HEAD
$ ls
file-
Run Code Online (Sandbox Code Playgroud)
结果:所有已暂存但未提交的文件已删除0_o
git reset --hard HEAD\^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
Run Code Online (Sandbox Code Playgroud)
有什么办法可以恢复我刚刚删除的文件吗?换句话说,是否可以将git存储库恢复到git add -A
发出命令之前(或何时)的条件?
pok*_*oke 20
是的,你真的很幸运.你在某种程度上添加了Git的索引.实际上,当Git添加到索引时,它确实为每个文件创建了blob对象.索引本身仅存储树对象.
所以,是的,为您的分阶段文件创建了blob对象.您丢失的只是树信息,即路径和文件名,但您可以恢复内容.
尝试运行git fsck
,你应该得到一个悬挂blob列表:
Checking object directories: 100% (256/256), done.
dangling blob ac28af8d84fc71eb247ccf665c6d0f4bf1822520
dangling blob 2d152ff9f09cb08ebc495f453da63eddaa9e249f
dangling blob cd9567427762cd8066b4e802e5c170a31a026100
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过执行来恢复内容git cat-file -p ac28af8d
.例如,您可以将其传递给文件:
git cat-file -p ac28af8d > recovered-file
Run Code Online (Sandbox Code Playgroud)
为他们所有人做这件事,然后让他们回来.