How to undo git rm -rf dirname without a first commit?

B Seven picture B Seven · Mar 10, 2012 · Viewed 9k times · Source

I did:

git init
git add .
git rm -rf dirname

Looking at other answsers, git reset --hard HEAD, git checkout -f and git reflog did not work, apparently because there is no HEAD to go back to, nor a commit to get the files back from.

Is there a way to get the files back?

Answer

Lambda Fairy picture Lambda Fairy · Mar 10, 2012

There is no way.

Usually, git rm checks the files have already been committed before deleting them, so you don't lose any of your work. However, using -f overrides this check.

In short:

  • Don't use -f.
  • Don't touch anything you haven't committed.