Last Updated: February 25, 2016
·
425
· matheus-de-souza

Recover a deleted file in git history

Firstly, get all deleted files:

git log --diff-filter=D --summary

Then, find the desired file:

grep "/filename$"

Keep only the filepath:

sed "s/ [^0-9]\+ [0-9]\+ \(.*\)$/\1/"

All together:

git log --diff-filter=D --summary | grep "/filename$" | sed "s/ [^0-9]\+ [0-9]\+ \(.*\)$/\1/"

Choose the correct filepath and finally restore it:

git checkout your_branch -- filepath

Only the commands:

git log --diff-filter=D --summary | grep "/filename$" | sed "s/ [^0-9]\+ [0-9]\+ \(.*\)$/\1/"

git checkout your_branch -- filepath