Find when a file was deleted in Git

Viewed 363041

I have a Git repository with n commits.

I have a file that I need, and that used to be in the repository, and that I suddenly look for and think "Oh! Where'd that file go?"

Is there a (series of) Git command(s) that will tell me that "file really_needed.txt was deleted at commit n-13"?

In other words, without looking at every individual commit, and knowing that my Git repo has every change of every file, can I quickly find the last commit that HAS that file, so I can get it back?

9 Answers

You can find the last commit which deleted file as follows:

git rev-list -n 1 HEAD -- [file_path]

Further information is available here

This command didn't work for me:

git log --full-history -1 -- [file path]

This is the command that worked:

git log --follow -- [file path]

This works for me:

git log -- **/Myfile.cs

If include --full-history switch, it will show a lot of changes have nothing to do with the file. Not sure why.

One can do the same for a directory if one doesn't recall the exact filename. Find the commit where deletion occurred and checkout a commit before that.

git log -- the/dir/where/things/got/deleted/
Related