简体   繁体   English

Git Repository如何恢复已删除的文件?

[英]Git Repository How to recover deleted files?

I was trying to push my project to a new repository. 我试图将我的项目推送到新的存储库。 Unfortunately, I accidentally removed the project before the commit was complete. 不幸的是,我在提交完成之前意外删除了项目。 All the files in the project directory were deleted. 项目目录中的所有文件都已删除。 How can I recover the files? 我该如何恢复文件?

$ git log --diff-filter=D --summary | grep delete
 delete mode 100644 .gitignore
 delete mode 100644 LICENSE
 delete mode 100644 README.md
 delete mode 100644 app/__init__.py
 delete mode 100644 app/auth/__init__.py
 delete mode 100644 app/auth/forms.py

If the deletes have not been committed, perform a checkout on all of the files to go back to the last commit. 如果尚未提交删除,请对所有文件执行checkout以返回上次提交。 Any changes after the last commit are however lost. 但是,上次提交后的任何更改都会丢失。

git checkout .gitignore
git checkout LICENSE
git checkout README.md
git checkout app/__init__.py
git checkout app/auth/__init__.py
git checkout app/auth/forms.py

If they have been committed you will need to revert to the previous commit where, supposedly, these files existed. 如果它们已经提交,您将需要恢复到之前的提交,据称这些文件存在。 Note, this will remove any other changes you made, not just restoring these files. 请注意,这将删除您所做的任何其他更改,而不仅仅是还原这些文件。

git reset --hard <SHA hash of commit where the files existed>

作为JHowIX答案的变体:如果你没有提交删除并想要取消删除工作目录中的所有文件,而不删除某些文件,你可以简单地使用

git checkout .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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