Restore a Deleted File in Git

Today I found out that we deleted a file in one of our projects that we actually needed.  We deleted it a few weeks ago and therefore I had no idea what commit it was a part of or how to actually get a hold of it.  The process for retrieving this file in Git is incredibly easy.

To restore a deleted file in Git, say one that existed at “Source/deleted-git-file.txt” in my repository, first we need to find what commit we deleted it in.  To do this, we use the Git rev-list command:

git rev-list -n 1 HEAD -- Source/deleted-git-file.txt

This will output a commit hash that might look like this:


That represents the commit where the file was deleted.  To retrieve the file, we will do a git checkout on the previous commit for the specific file.  That looks like this:

git checkout 6d8e24d4236627a35f56f32947ae5593fc917880^ -- Source/deleted-git-file.txt

That will checkout that file into your working copy as a new file.  You will then need to git add and then git commit them.

Big thanks to Stack Overflow for providing the solution to this!

This entry was posted in Development and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted May 4, 2011 at 8:30 am | Permalink

    I must express my appreciation to the writer just for bailing me out of this problem. Right after scouting through the online world and finding ways that were not beneficial, I thought my entire life was done. Living minus the answers to the problems you have fixed by means of your review is a crucial case, as well as the ones that could have in a wrong way affected my entire career if I had not come across your website. Your good skills and kindness in maneuvering the whole thing was precious. I am not sure what I would have done if I hadn’t encountered such a solution like this. I’m able to at this time look forward to my future. Thank you so much for your expert and effective help. I won’t think twice to refer the website to anyone who would like guidance about this subject.

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>