0

I accidentally used git reset -hard on my files in server and it had files which are not added or committed using git add or git commit. How can I restore those files which are not staged? Please help.

  • 1
    If they were not commited you cannot restore them because they were not managed by Git. If you were using an IDE you may try its local history. – rbento Apr 04 '19 at 12:34
  • 1
    Unfortunately they're gone unless you're using an IDE like netbeans or eclipse that track files. You can look at history in them. – EncryptedWatermelon Apr 04 '19 at 12:35
  • 1
    *files which are not added or committed* should not be touched by `git reset`. – phd Apr 04 '19 at 12:35

2 Answers2

1

No, you can't recover your files with git.

It would have been:

Easy, if you have stashed or committed your files. difficult but possible, if you have just staged them. But impossible in your case :-(

Recovery tools are your only last hope but if you already tried them unsuccessfully, I'm sorry to tell you that it's probably definitely lost...

You just learned the hard way the first rule of git:

If you are not sure of what you will do, commit (to create a backup point) and after try to do your stuff.

Pankwood
  • 1,799
  • 5
  • 24
  • 43
1

You will have to restore from a backup. If you don't keep backups, you will have to first attempt using a data recovery utility (or failing that recreate the files from scratch), and then take this as a learning experience and start keeping backups.

Although git is very good at protecting and retaining the data you commit, it can do nothing for data you don't commit. The worktree is not protected and there are git commands that are destructive to data in the worktree.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52