Can deleted files that are added but not committed in Git be recovered? [duplicate]

余生颓废 提交于 2019-12-19 03:24:30

问题


I am new to git, and I just made a stupid mistake, that I deleted some important files by "rm *" command. However, I did use "git add" to add those files, but not commit. the deletion is not added yet. So is there any way to recover these deleted files.

Any suggestions or answers would be appreciated. Thanks.


回答1:


Since the files are already in your staging area (index), simply commit them using git commit without any arguments. This will commit the contents of the index regardless of the current state of the working tree.

Then you can use git reset --hard (if you don't have other changes in the tree you want to keep) or git checkout FILE1 FILE2... to restore the lost files into your working tree.




回答2:


You can use git fsck --lost-found to find all objects that are no longer referenced. You'll want to look at 'blob' objects and see if any of them match the files you deleted. Hopefully you don't have many unreferenced objects lying around.


As user4815162342 pointed out, the poster actually said the deletion had not been added yet, which means they can be retrieved with git checkout, or simply committed right now to be sure they don't go away.

I'm going to leave my answer up in case anyone else wants info on git fsck --lost-found, but user4815162342's answer should be accepted.



来源:https://stackoverflow.com/questions/12434618/can-deleted-files-that-are-added-but-not-committed-in-git-be-recovered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!