Recover dangling blobs in git
See here: http://schacon.github.com/git/user-manual.html#dangling-objects
For commits, you can just use:
$ gitk <dangling-commit-sha-goes-here> --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 <dangling-commit-sha-goes-here>
For blobs and trees, you can’t do the same, but you can still examine them. You can just do
$ git show <dangling-blob/tree-sha-goes-here>
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.
You can use git show fce4a77b63b25abbc010859b4037589983820329
to 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.
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.
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