0

I add some new files to index

git add .

then reset

git reset --hard

git deleted these files in my working tree, there are not in repository previously, they are all lost. I lost all my data.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
yuanjianpeng
  • 318
  • 2
  • 11
  • See https://stackoverflow.com/a/5788069/6330106. You can still find the contents of the added files. But the directory structure and filenames are lost. – ElpieKay May 21 '19 at 07:28
  • 2
    Do you have a question? If you wanted to keep uncommitted things, why did you hard reset? – jonrsharpe May 21 '19 at 07:28
  • I want undo add. but keep the files in working tree. I run a wrong command, I should use --mixed @jonrsharpe – yuanjianpeng May 21 '19 at 07:33
  • @yuanjianpeng Yes, and since `--mixed` is implied, `git reset` would be enough. Losing data is indeed frustrating in the short term, but sometimes you have to burn your hand once to learn a lesson... – Romain Valeri May 21 '19 at 07:38
  • If there is a small chance you might need to recover the contents of your worktree, you could use `git stash` instead of `git reset --hard HEAD`. This would similarly remove all changes to tracked files in your index/worktree, but it keeps those changes in the stash. Only run `git reset --hard HEAD` if you are really really sure that you will not need the changes you are about to discard. – Alderath May 21 '19 at 08:49

1 Answers1

1

from the git documentation:

--hard Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

so this works exactly as documented. You probably wanted to do

git reset

without the --hard option.

See Recover from git reset --hard? if you want to try to recover your code.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136