1

What I did is as follows:

$ git init
$ git add .
# bunch of unwanted files were also added
$ vim .gitignore
$ git rm -rf *
$ git add .
# but the files were actually deleted from the ./ folder.

Now what i have in the directory are only the .gitignore pattern files. All the necessary files are gone.

As mentioned in this post How to revert a "git rm -r ."?

is git reset HEAD way to go?

I am not sure i should run this command as i have not made any commits till now.

Sudip Bhattarai
  • 1,082
  • 9
  • 23

1 Answers1

4

Run git fsck --lost-found:

$ mkdir tt
$ cd tt
$ git init
Initialized empty Git repository in ...
$ echo I am foo > foo
$ echo I am bar > bar
$ git add .
$ git rm -rf *
rm 'bar'
rm 'foo'
$ git fsck --lost-found
notice: HEAD points to an unborn branch (master)
Checking object directories: 100% (256/256), done.
notice: No default references
dangling blob 2adbcadb36527b3048a96c5bad232f6c9e762524
dangling blob bea14010b78ced155c64da9a4c2b1a6b0831335e
$ ls .git/lost-found/other/
2adbcadb36527b3048a96c5bad232f6c9e762524
bea14010b78ced155c64da9a4c2b1a6b0831335e
$ cat .git/lost-found/other/2adbcadb36527b3048a96c5bad232f6c9e762524 
I am foo

The file's names are gone, but all their contents are restored, to their hash-ID based names.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Needn't restart a week of work. :) it worked. I was looking through the files in the .git directory, but the lost-found appears only after the `git fsck` command. – Sudip Bhattarai Jul 24 '17 at 16:46
  • Yes - before that, these "dangling blob"s are in the .git/objects/ directory and are in compressed, Git-ified form. – torek Jul 24 '17 at 16:49
  • After trying various 'undelete'-tools for linux this was the only possible and working solution for me. – justMartin Jul 27 '18 at 06:02