简体   繁体   中英

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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