LeonF
LeonF

Reputation: 433

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

Upvotes: 1

Views: 5283

Answers (2)

werkritter
werkritter

Reputation: 1689

As a variant of JHowIX's answer: if you didn't commit the deletions and want to undelete all files in the working directory, without leaving some files deleted, you can use simply

git checkout .

Upvotes: 1

JHowIX
JHowIX

Reputation: 1803

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>

Upvotes: 2

Related Questions