Search code examples
gitgithubgit-resetrecover

Recover initial files after git reset --hard HEAD~


Please help! I've been working between several computers and wanted to keep track of my non-programming schoolwork. I set up an initial repository on Github, initiated that on my local machine, and then proceeded to add all files in my school directory, commit those files, and then attempted to push them to the repository. However, I did not realize how large the GIS files were, so the push kept failing. I wanted to go backward so that I could create a .gitignore file to skip over the large files, and tried to following command to clear out the commit:

git reset --hard HEAD~

I had no clue that this wouldn't just untrack the files, but delete them from my file system. Now I have lost my entire school directory.

Can anyone suggest how to recover any of the files? I can't find an answer elsewhere.

EDIT: The answer below worked great, here's the result:

D:\Documents\College\Masters\[MyUniversity]>git reflog
99b0b97 (HEAD -> master, origin/master) HEAD@{0}: reset: moving to 99b0b97
99b0b97 (HEAD -> master, origin/master) HEAD@{1}: reset: moving to HEAD~
a291e5a HEAD@{2}: reset: moving to HEAD
a291e5a HEAD@{3}: commit: added all files
99b0b97 (HEAD -> master, origin/master) HEAD@{4}: commit (initial): first commit

D:\Documents\College\Masters\[MyUniversity]>git reset --hard a291e5a
Updating files:   0% (37/8608)            

Solution

  • get the revision of the commit using git reflog and then reset to it:

    git reset --hard the-id-of-the-commit-from-reflog