【问题标题】:Recover directory removed with git rm -rf恢复使用 git rm -rf 删除的目录
【发布时间】:2016-07-09 19:55:06
【问题描述】:

我使用git add 将目录添加到本地git repo,然后不小心做了git rm -rf(之前没有提交)。有没有办法从这个目录中恢复文件?

【问题讨论】:

  • 在下面给你写了一个完整的答案。
  • 如果您删除了整个目录(包括 .git 目录),您似乎需要使用一些数据恢复软件 - 尽量不要在黄昏时放置新文件(尤其是在此目录中),直到您尝试恢复数据。 superuser.com/questions/279733/undo-an-rm-rf-command

标签: git


【解决方案1】:

是的,你可以,

阅读:How to undo changes in Git


一旦您将文件添加到 git,它们就会开始被跟踪并存储在 .git 文件夹下。

在它们刚刚添加并且从未提交时,它们被称为dangling objects。这些对象可以恢复。


如何恢复悬空物体?

首先我们必须找出它们

git fsck --full

获得这些对象的列表后,您需要查看它们并找出您需要的对象。

# Print out the content of the Object
git cat-file -p <SHA-1>

# If the object is a file you will simply see its content,
# Copy it and save it in a new file.

【讨论】:

  • 很高兴为您提供帮助,阅读附件中关于 HEAD 的文章,我认为它会教给您新的东西。
  • 拯救了我的一天!谁想要下一步恢复?
  • 我欠你一杯啤酒!
【解决方案2】:

这是我的 recover.py 实用程序,它将为每个哈希创建一个文件:

enter code here


import subprocess
hashes=["01b6a7f272375bc99d98be0068c273b5bc4e9ff6",
"03620d7b53c8a48314c25a3ecdbe369553b01340","PUTYOUR HASHEZ HERE"]
for myhash in hashes:
    print "HASH", myhash
    bashCommand = "git cat-file -p "+ myhash
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()
    file = open("recover/"+myhash, "w")
    file.write(output)
    file.close()

这将写入恢复文件夹中你用 git 杀死的所有文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2012-10-05
    • 2011-06-28
    • 2019-03-08
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多