恢复git reset --hard删除的文件

##吐槽

在说恢复方法之前,先说下缘由。今天在转载完缓存算法之后,就打算把文件给同步到github上,以做备份。然后像往常一样打开git bash,在git add时遇到了错误,无法进行git add,因为我使用了hexo new进行生成文件,而hexo new生成的文件中,文件名的小括号和中括号都会变成’–‘,也就是说[转载]变成了-转载-,而git bash无法识别-,所以造成无法提交,这时,我灵机一动,想到将其他的所有文件都移出去,然后git add .来添加然后再提交(因为其他文件有改动,所有想把此文件单独提交后再来进行全部的提交),移出之后我发现原来已经执行过git add .了。没办法,那就重置吧,但是我从网上找来一篇文章,git reset --hard,再然后,悲剧上演~

所以,骚年们,以后一定不要那么随便的相信一篇博文,要对知识抱有一定的怀疑态度,然后多看几篇博文甚至官方文档。不过最终从stackOverflow上找出了解决方法~

##恢复已提交文件的方法

##恢复未提交文件的方法

下面是stackoverFlow上一些人提供的方法

If you didn't already commit your local changes (or at least stage them via `git add`, they're gone. `git reset --hard` is a destructive operation for uncommitted changes.

If you did happen to stage them, but didn't commit them, try `git fsck --lost-found` and then search through the contents of .git/lost-found - it will contain all of the objects that aren't referenced by a known commit, and may include versions of files that were staged.

You can recover anything you git added, with git fsck --lost-found and poke around in .git/lost-found.  find .git/objects -type f | xargs ls -lt | sed 60q will give you the last 60 things to get added to the repo, that'll help.

Anything you didn't git add is gone as surely as if you'd deleted it yourself.

我总结一下吧,其实呢,就两步:

好了,以上。对了,忘了说了,恢复未提交的文件仅仅是git add过的,如果你连git add也没有执行,那么我就不晓得怎么做了,如果你知道的话,请告诉我,另外,一定要记着,没事不要执行git reset --hard~然后网上的教程什么的也不要轻信,要时刻对知识抱有一种怀疑的态度,包括本文~


版权声明 本人博文若无特别说明,均由voidy-小鱼原创,若要转载,请附上作者以及博文链接 由于本人水平有限,所以难免有错,若发现错误,请在评论区任意吐槽~ 博文链接:http://voidy.net/