Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albertpark/7380ff1908adec5719b2e97c0cde38e2 to your computer and use it in GitHub Desktop.
Save albertpark/7380ff1908adec5719b2e97c0cde38e2 to your computer and use it in GitHub Desktop.

Recover Deleted Files in My Local Repository using Git

You can view a list of all the deleted files using the command.

$ git ls-files --deleted

Use this this command to restore your deleted file. Replace the <deleted-file> with the deleted filename.

$ git checkout -- <deleted-file>

If the deletion has been committed, find the commit where it happened.

$ git rev-list -n 1 HEAD -- <deleted-file>
b6e9c0b612d71a83bfae3a1afc5c252f53165896 # The commit hash

Then recover the file from this command. Replace the <commit-hash> with the actual hash that was returned from the previous command.

$ git checkout <commit-hash> -- <deleted-file>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment