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.

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>