1

I performed the below steps -

  1. Initialize a new git repository using git init.
  2. Add some files to it.
  3. Perform git add.
  4. Delete a file using git rm -f <fileName>

How to retrieve the deleted file above here? Kindly note there are no commits yet.

5
  • 2
    you can't, it is gone. Commented Jul 12, 2022 at 11:54
  • stackoverflow.com/a/57652357/5170424 try this Commented Jul 12, 2022 at 11:56
  • 1
    One hope you have is that the file is still in the filecache of your editor. I had the luck once to find a needed file in the filecache of Notepad++.
    – David
    Commented Jul 12, 2022 at 11:56
  • @ShridharPatil There is no commits. Commented Jul 12, 2022 at 12:00
  • Always commit early and often Commented Jul 12, 2022 at 12:01

1 Answer 1

2

Since you have run git add, the content of the file has been written in git objects store.

Run the following command :

git fsck --full --unreachable
# in the output, you should see lines looking like :
unreachable blob 08bf360988858a012dab3af4e0b0ea5f370a2ae8
unreachable blob 21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89
unreachable blob 08c12ef37075732cf4645269ab5687ba6ba68943
...

Since you have no commit, you should have only a few lines like the above. You can inspect the content of each blob (a blob is a file in git parlance) :

git show 08bf360988858a012dab3af4e0b0ea5f370a2ae8

Once you have found the one you expect, just redirect the output :

git show 21bf7ea93f9f9cc2b3ecbed0e5ed4fa45c75eb89 > back-from-the-dead.txt

I have posted a few more details in this other answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.