Reputation: 12292
I accidentally deleted a project with a .git/ in it. There is only this local repository.
I recovered many files using recuva
and the project is restored there. But the .git-things aren't back, because I only have a single folder containing all the files: http://pastebin.com/sBiQ9fin
and I don't know where to put them.
Is it possible to put those files back in a .git/ folder so that I can restore all the commits etc of the project?
Upvotes: 2
Views: 629
Reputation: 177574
If you have recovered the loose object files and only have the 38-char suffix of their names, it’s easy to get the full name back. I wrote this Python script:
import os
import hashlib
import sys
for name in sys.argv[1:]:
with open(name) as obj:
contents = obj.read().decode('zlib')
sha1 = hashlib.sha1(contents).hexdigest()
assert sha1[2:] == name
path = sha1[:2]
os.renames(name, '.git/objects/{0}/{1}'.format(path, name))
First run git init
in a directory with all the objects, then invoke it with
python recover.py ??????????????????????????????????????
Afterwards, run git fsck
to verify the loose object database. If you were successful, it will tell you which commits are dangling, like
josh@tengwar:~/test (master)$ git fsck
notice: HEAD points to an unborn branch (master)
notice: No default references
dangling commit 0170886ebc339424aab2e1685a32a2de2ce62e13
dangling commit 6451cd01f1f76b224352f1d2d0fca12a21454c3e
dangling commit 1e13a551382e652fa07a108341516f0f4a441c9c
dangling tag 7914223b1bb0d0e9179027a00bc2f62e118483d4
dangling tag 2895de51d79904d707dfbf5bcd68b047e49c9f03
dangling commit 8156f571839e5f42d043dcb6bd91aa406e909f76
dangling commit 75b7d8d60344b576b19cc7908d180757d50274c3
dangling tag 9aed839bbc4e3c5cd031d73b78ea87b43688e34c
dangling commit 69fe3581a8b06266c294d72e5944e2415d5af612
Then you can create a branch pointing to any of the commits with git checkout <id> -b newbranch
, and all will be good. (If git fsck
reported missing objects, things will be difficult.)
Upvotes: 2
Reputation: 37441
It doesn't seem likely you'll be able to recover anything more. To avoid this happening again, I'd recommend getting a repo set up on one of the various code hosting sites (github, gitorious, repo.or.cz). They have support for private repos if you need, some free and some not.
Also, disk backups are another method to keep the code safe.
Upvotes: 2
Reputation: 150605
If that is all you have it is unlikely. Those are not the full sha addresses (38 char instead of 40) because the first two characters are the directory name that those objects belong to.
Unless you have something else.
Upvotes: 1