Home > Software engineering >  recover files due to mistakes with git
recover files due to mistakes with git

Time:05-27

I made the following steps:

  • I had some files F1, F2, F3, ... in a folder F initialized with git clone from a remote empty repository
  • I executed git add .

Then I made two mistakes:

  • first, I ran in F: git reset --hard HEAD
  • second, I removed the .git folder in F

Now the directory F is empty and I have lost all my files. Is there any way to recover the files?

CodePudding user response:

Is there any way to recover the files?

No.

This was an interesting perfect storm of errors. Saying

git reset --hard HEAD

is very violent; it means, restore everything in the working tree to be exactly the same as the most recent commit. All uncommitted changes are thus erased.

Fortunately, however, a hard reset does not touch untracked files. So the new files you created should still be there, right?

Wrong, because you first said

git add .

This turned your new untracked files into tracked files! So the hard reset erased them completely.

The files were never committed so they were lost completely. But even if they had been committed, you would then have thrown that commit away when you deleted the .git folder!

So basically you erased your files without backing them up first, and then, just in case you had backed them up, you threw away the backup.

  •  Tags:  
  • git
  • Related