Say I\'m in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.
I know
git undelete path/to/file.ext
Put this in your .bash_profile
(or other relevant file that loads when you open a command shell):
git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1" -'
Then use:
git undelete path/to/file.ext
This alias first checks to find the last commit where this file existed, and then does a Git checkout of that file path from that last commit where this file existed. Source.
I also have this problem using the below code to retrieve a previous file to a local directory:
git checkout <file path with name>
The below example is working for me:
git checkout resources/views/usaSchools.blade.php
To restore all those deleted files in a folder, enter the following command.
git ls-files -d | xargs git checkout --