I committed a file to my local git and then pushed it to github.

I performed the following commands to remove that file and then pushed it to github:

git rm file.txt
git commit -m remove file.txt
git push -u github master

I then realized that I want the file back. I tried the instructions outlined here: https://www.quora.com/How-can-I-recover-a-file-I-deleted-in-my-local-repo-from-the-remote-repo-in-Git

I ran the following command:

git rev-list -n 1 HEAD -- file.txt

which gave me a hash for the version which had my file: HASH. Which I then used in the following command:

git checkout HASH^ file.txt

Which then gave me the following error:

error: pathspec 'file.txt' did not match any file(s) known to git

Luckily, I was able to go to github and recover all of my files from there, but I still don't known how to do it locally, or even if it's possible.

1 Answer 1


Use git reflog to get the commit hash for the point that had your file. Then use git checkout <hash> to get back to that commit hash.

Alternatively, you can use git checkout HEAD{1} to go back one commit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.