Finding and Recovering lost files in Git

One problem in git is that when a user deletes a file accidentally, git acts as though the user intended to do that. The next time the user runs "git commit -a" then the list of transactions will include the formal deletion of the file. People who don't inspect the file list, or who carelessly run "git commit -a -m 'your message'" will delete files.

Today I had that situation arise and I have a brief report on how I fixed it. If you don't know for sure what the file path was, first scan all deleted files to see if you spot the ones you want:

$ git log --diff-filter=D --summary | grep delete

If the list is huge, grep for an element in the file name or path you are looking for. In my case, the missing file was an ogv file:

$ git log --diff-filter=D --summary | grep delete | grep ogv

delete mode 100644 36.LaTeX_Overview/LaTeX-Overview.ogv
delete mode 100644 37.LyX-for_LaTeX_homework/LyX-LaTeX_homework.ogv
delete mode 100644 46.windows_R_setup/46.windows_R_setup-slides.ogv

I had 3 files to restore. Now I know their full paths.

Find out which commit removed the files (I've sanitized the name of the author of the commit):

$ git log --all -- 37.LyX-for_LaTeX_homework/LyX-LaTeX_homework.ogv
commit b67eaa335379d5bb5e1187482d3647000426644d
Author: Anonymous
Date: Thu Sep 27 15:43:43 2018 -0500

Finished the installing dependencies and R sections.

There are 2 ways to recover files. These files happened to be in an LFS-enabled git repo, so the first method I tried retrieved the "reference" version of the file. I was not sure I could trust that. This method retrieved the actual ogv file:

$ git checkout b67eaa33^ -- 37.LyX-for_LaTeX_homework/LyX-LaTeX_homework.ogv

The ^ means "the one before this" because the one before this commit was the last one that had the file.

The other method of retrieving uses git show. In my git instruction manual (, we have illustration of the show method.

About pauljohn

Paul E. Johnson is a Professor of Political Science at the University of Kansas. He is an avid Linux User, an adequate system administrator and C programmer, and humility is one of his greatest strengths.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply