3

I created a file then staged it in my git repo using git add <file>.

I also wanted to remove some files in the same commit so did a git rm -f 'files.*'.

The wildcard I used in the remove line was a little too generic, and it removed my previously staged file.

Bearing in mind it was never committed, only staged, is there any way of retrieving the file I inadvertently removed?

Fordio
  • 3,410
  • 2
  • 14
  • 18

1 Answers1

1

If you had staged a file to git, you likely get it back;

  1. Try git fsck --lost-found

The output will be something like;

$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
dangling blob 20d25014d965f1fe519d53204222369d09503511
dangling blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
  1. One of the dangling blob should be your file. Try git show <sha> to see the file.

Note: Do NOT run git gc before the above steps. It cleans up unnecessary files and optimize the local repository.

Alper
  • 12,860
  • 2
  • 31
  • 41