简体   繁体   English

恢复 git 存储库/提交等

[英]Restore git repository/commits etc

I accidentally deleted a project with a.git/ in it.我不小心删除了一个包含 a.git/ 的项目。 There is only this local repository.只有这个本地存储库。

I recovered many files using recuva and the project is restored there.我使用recuva恢复了许多文件,项目在那里恢复。 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.但是.git-things 没有回来,因为我只有一个包含所有文件的文件夹: http://pastebin.com/sBiQ9fin我不知道把它们放在哪里。

Is it possible to put those files back in a.git/ folder so that I can restore all the commits etc of the project?是否可以将这些文件放回 a.git/ 文件夹中,以便我可以恢复项目的所有提交等?

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).为避免再次发生这种情况,我建议在各种代码托管站点之一(github、gitorious、repo.or.cz)上设置一个 repo。 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.此外,磁盘备份是另一种保证代码安全的方法。

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.如果您恢复了松散的 object 文件并且只有 38 个字符的名称后缀,则很容易找回全名。 I wrote this Python script:我写了这个 Python 脚本:

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首先在包含所有对象的目录中运行git init ,然后调用它

python recover.py ??????????????????????????????????????

Afterwards, run git fsck to verify the loose object database.然后,运行git fsck以验证松散的 object 数据库。 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.然后,您可以使用git checkout <id> -b newbranch创建指向任何提交的分支,一切都会好起来的。 (If git fsck reported missing objects, things will be difficult.) (如果git fsck报告缺少对象,事情会很困难。)

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.这些不是完整的 sha 地址(38 个字符而不是 40 个),因为前两个字符是这些对象所属的目录名称。

Unless you have something else.除非你有别的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM