通常,您可以执行以下操作:
$ 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
我认为自己做了一件非常糟糕的事情:
$ 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-
结果:所有暂存但未提交的文件都已删除0_o。
git reset --hard HEAD\^
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree.
我刚才删除的文件能否恢复?换句话说,是否可以将git仓库还原到git add -A
命令执行之前(或当时)的状态?
git show
作为git cat-file -p
的替代方法。非常好的答案。我自己没有意识到这一点。 - Michael Miorcat-file -p
,因为它可以为任何对象类型提供相同的输出 ;) - pokereset --hard
是摆脱索引的众多方法之一... ;) - pokels -lrt .git/objects
显示最新的前缀,然后在对象目录中再次执行,例如ls -lrt .git/objects/f8
。最后,执行git cat-file -p f8<rest of name>
。 - mik3ygit fsck --lost-found
命令。这将把所有的悬空 blob 写入到.git/lost-found/other
目录中。然后,您可以使用less .git/lost-found/other/*
命令浏览文件,并使用:n
(下一个)和:p
(上一个)进行切换,或者通过 grep 命令查找您要查找的文件。 - user149408