Yujie
Yujie

Reputation: 445

git deleted by mistake, how to restore it?

I just executed git reset --hard by mistake. All my files are lost. How can I recover them? There are a lot of new files that I wrote here. If I can't restore them, then I will be miserable.

When I use the git reflog cpmmand it shows:

e1074bb HEAD@{0}: checkout: moving from master to workbranch
e1074bb HEAD@{1}: clone: from https://gitee.com/yujie-cui/gem5.git

Why doesn't it record my reset operation?

What can I do now?

Upvotes: 0

Views: 78

Answers (1)

matt
matt

Reputation: 534885

I just executed git reset --hard by mistake. All my files are lost. How can I recover them? There are a lot of new files that I wrote here. If I can't restore them, then I will be miserable.

Some potentially destructive things you do with Git give you a chance to change your mind first. Others just do it; Git assumes you know this is dangerous and just obeys. reset --hard is one of those.

reset --hard returns your working tree and your index to the exact state of the HEAD commit, and if you had any local changes (new files, edits, whatever) that you never committed, they are absolutely gone. If you did some other sort of backup, such as Time Machine on a Mac, maybe they are there. Or, as you've been told in a comment, maybe you're working in an IDE that keeps a separate historical record. But that is not the purview of Git.

The lesson here: always add-and-commit before doing just about anything. The only thing Git always preserves for you is commits.

Upvotes: 2

Related Questions