Koenig Lear
Koenig Lear

Reputation: 2436

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?

Upvotes: 0

Views: 811

Answers (2)

simon-pearson
simon-pearson

Reputation: 1970

  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

Upvotes: 2

Mike
Mike

Reputation: 1343

Have you tried

// It will reset your commit back to previous if its 2 commits back then use head~2 etc.
git reset head~1

Upvotes: 0

Related Questions