Recover dangling blobs in Git

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

I've recovered them in the past using the fsck command with the lost and found option:

> git fsck --lost-found

Running that then saves the dangling blobs to the following path

> .git/lost-found/other

Solution 3 - Git

See here: http://schacon.github.com/git/user-manual.html#dangling-objects

> For commits, you can just use: > $ gitk --not --all > > This asks for all the history reachable from the given commit but not from any branch, tag, or other reference. If you decide it’s something you want, you can always create a new reference to it, e.g., > $ git branch recovered-branch > > For blobs and trees, you can’t do the same, but you can still examine them. You can just do > $ git show > > Usually, dangling blobs and trees aren’t very interesting. They’re almost always the result of either being a half-way mergebase (the blob will often even have the conflict markers from a merge in it, if you have had conflicting merges that you fixed up by hand), or simply because you interrupted a "git fetch" with ^C or something like that, leaving some of the new objects in the object database, but just dangling and useless.

Solution 4 - Git

Here's a script to convert the blobs to readable, .txt files. Use the other answers (e.g., user909746's answer) to create and navigate to the lost-found folder.

From within the lost-found folder, run:

for FILE in *; do git show $FILE > "$FILE.txt"; done

This will create a .txt copy of every blob and add it to the current directory, which is the lost-found folder. You can now filter using your file explorer of choice for .txt files and browse them all to find the files you need.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZackary ParsonsView Question on Stackoverflow
Solution 1 - GitVi.View Answer on Stackoverflow
Solution 2 - Gituser909746View Answer on Stackoverflow
Solution 3 - GitRicardo SouzaView Answer on Stackoverflow
Solution 4 - GitherteladrianView Answer on Stackoverflow