Find and Restore deleted file on git

If you have ever neglected some legacy code for a while and then gone back later to restore it, the most annoying part is if you deleted a file that you needed in the back end. I was tasked with something similar and found that git saved the day but getting the commands right was a little tricky, so I thought i’d share them.

git log --diff-filter=D --summary | grep delete
git log --all -- FILEPATH
git show <SHA> -- <path-to-file>

The first command shows the paths of the files deleted across all branches in the git repo. Placing the file path in the second command, will give you the commits and SHA for what resulted in the deletion of that  and then using them in the third, will give you the file you need.

 

Hope this helps!!!

Leave a Reply

Your email address will not be published. Required fields are marked *