Git – Recover dangling blobs in Git


I did a "git rm -rf ." (trying to purge the cache of files I had removed after doing "git add .") without thinking git would physically delete the files. I don't have an initial commit/branch yet.

$ git init
$ git add .

I remember to add my ".gitignore". Then, out of being lazy and also not bothering to look up the proper command I did:

$ git rm -rf .

Now every file that git was tracking is gone. Oops.

How do I recover the files using the dangling blobs

$ git fsck
notice: HEAD points to an unborn branch (master)
notice: No default references
dangling blob 45cb2316b079df7898a28bab1389c87d37de4da5
dangling blob 06b9d0f91bb0643a54ec027efc0efd6645d95326
dangling blob c6c828230bc129da40928d55297577421c6f2e79
dangling blob 087b296f5ae80151fb0d6150927ebe8049ed7706
dangling blob c957743cb7ea533ce772d178394ce9f656b17b7c
dangling blob 0ea745612b105394f5cd5c0119a829de98a0a954
dangling blob 8fb1fa03c12cd56d4e2284008e92a3666bc60b93
dangling blob cf49a6cf77ac792b108577c01ccf88cb2c28228c
dangling blob 93817d9eeefea6ea894544e78b0e0970aa5adc46
dangling blob 94a9ed024d3859793618152ea559a168bbcbb5e2
dangling blob 574b7130959f2278c88946cf385fc49adcdf28c2
dangling blob 582f5bceb8b35c01fd7442678a3cd7e1088b7957
dangling blob d9fb7ca5775745b4332f630400d52c979aab35cd
dangling blob 2087b182bf8b4b8d5ed8fe45a50c7661bda25feb
dangling blob 6109baf32d04410c84d860501057a7f55a13f9dd
dangling blob 22cc2ac7585b1d47bccf2ecb7bf57e16c2142bb6
dangling blob 63b329443cc27273fad14c1e41de5bb9bf1bdd03
dangling blob a2296d429715c9b6d730fdcc972eefdf8273d2e2
dangling blob e41aedd7b2dbdee8b965a30d40ed0c9275194984
dangling blob 6dc7b06e8f03cda72cf223b050d07ccd2b850fc8
dangling blob ee52d86d94a40e7092dc34d1b2760244ec7dccaf
dangling blob 2ffa08c086d3702563d498c9069a345940b3a348
dangling blob 6fafa07526ad288ff2f6e26810ed9818eb18aabb
dangling blob b1cdf17b5bb40b4839cfc80f7e91bbcf7b94f798
dangling blob 353db77b88c248c752cdbd914787b9ebdf2d700f
dangling blob f61d3492ce0d0e396fd322114a5e3475886de1cc
dangling blob 7756a8713095390f5b106b81ae7f7247f762970b
dangling blob bc995b7f9f6806dd5678227579a515bb5768d3a0
dangling blob fce4a77b63b25abbc010859b4037589983820329
dangling blob 3e3335f8cd27168d4fe81f61421d647f2905b7b0
dangling blob 7f56efed4f8706e5b79408afbde197964d824eab

Looking around I could only find tutorials on recovering dangling commits.

Best Solution

You can use git show fce4a77b63b25abbc010859b4037589983820329to see the content (or git show fce4a > somefile to dump it to a file).

File names are lost (unless there are also dangling trees or other sources of information like command history in .bash_history).

If you can see (with git ls-tree) your root tree, you can create a commit with it using git commit-tree command or some git checkout e48751c3b37a9cab692133202bbb933241f73f69 -- . to retrieve files.