How to recover deleted local files from Git repository?

I have a good habit of storing all my development projects and source code in a git repo.
I do commit often and once a new feature is implemented in development branch.
Will test for the feature and merge with master and push to remote git.

https://git-scm.com/images/logos/downloads/Git-Logo-1788C.png

This habit saved my life recently.

I am working on a python project.
When cleaning up the folders, wanted to delete all the python compiled files. *.pyc

I wanted to execute the command

rm *.pyc

But, My C key in keyboard was stuck and it was omitted.

The executed command was

rm *.py

 

Alas!

All my python files were deleted 😦

Git and my habit of committing often saved me here.

Searched for restoring deleted files from git and found the following link.
https://stackoverflow.com/questions/11956710/git-recover-deleted-file-where-no-commit-was-made-after-the-delete/11956744

The answer to restore all the deleted files from git repo is as below.

To recover all unstaged deletions at once, automatically, without specifying each single path:

git ls-files -d | xargs git checkout —

To recover all staged deletions at once, automatically, without specifying each single path:

git status | grep ‘deleted:’ | awk ‘{print $2}’ | xargs git checkout —

 

I used this command
git ls-files -d | xargs git checkout —

Thank God. All my python files are restored now.

My humble request to all my dear readers is,
“Make a habit of using Git or any version control. Do commit often, always. It will save your life for sure”

Thanks to Git developers for saving life of developers.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s