Search code examples
gitgit-revertrecover

recovering deleted files git after revert


I have a new repository. I added some files (a,b,c) via

git add .
git commit -m "x"

I realized file c was unnecessary so I looked for the last commit via

git log

with the commit id at hand I did

git revert commit-id

it started deleting all files removing file a b c

How can I restore them? I looked at the log and only the original commit x is in there.

I tried

git checkout HEAD^^ -- .

as per How do I "un-revert" a reverted Git commit?

but I get "Invalid reference HEAD^^"

any ideas?


Solution

    1. Get the commit hash of the initial commit where you added the files by doing a git reflog (this is essentially a log of all of the recent HEADs of your branch - very useful for scenarios like this).
    2. If you want to permanently go back to this commit and completely 'undo' your revert then do a git reset --hard <commit-id> replacing the hash with the hash of the initial commit where you added the files. If you want to temporarily go back to this commit then do a git checkout <commit-id>.

    Further reading on git reflog: http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html