git删除了所有内容,如何恢复文件和文件夹

Posted

技术标签:

【中文标题】git删除了所有内容,如何恢复文件和文件夹【英文标题】:git deleted everything, how to recover files and folders 【发布时间】:2013-04-30 20:07:50 【问题描述】:

我是第一次用git,想把一个已有的项目导入github,结果全部删掉了。在搜索答案后,我认为 git 在 git pull 之后删除了文件,我正在尝试恢复文件和文件夹,但我找不到如何去做。

我做了下一个:

jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git init
Initialized empty Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git add .
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   Catalogoapp/__init__.py
#   new file:   Catalogoapp/models.py
#   new file:   Catalogoapp/tests.py
#   new file:   Catalogoapp/views.py
#   new file:   Messageapp/__init__.py
#   new file:   Messageapp/models.py
#   new file:   Messageapp/tests.py
#   new file:   Messageapp/views.py
#   new file:   Ujixyapp/__init__.py
[...]

jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git add *
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   Catalogoapp/__init__.py
#   new file:   Catalogoapp/models.py
#   new file:   Catalogoapp/tests.py
#   new file:   Catalogoapp/views.py
#   new file:   Messageapp/__init__.py
#   new file:   Messageapp/models.py
#   new file:   Messageapp/tests.py
#   new file:   Messageapp/views.py
#   new file:   Ujixyapp/__init__.py
[...]
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git remote add origin https://github.com/PEREYO/Ujixy.git
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git pull origin master
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/PEREYO/Ujixy
* branch            master     -> FETCH_HEAD
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git push origin master
Username for 'https://github.com': PEREYO
Password for 'https://PEREYO@github.com': 
Everything up-to-date
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git init
Reinitialized existing Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git add *
jesus@jesus-K52F:~/Escritorio/Django/Ujixy$ git status
# On branch master
nothing to commit (working directory clean)

现在我正在尝试修复它做下一个:

jesus@jesus-K52F:~/Escritorio/Ujixy$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
dangling tree bfe11a30d57a0233d3b0c840a3b66f6421987304
jesus@jesus-K52F:~/Escritorio/Ujixy$ git status
# On branch master
nothing to commit (working directory clean)
jesus@jesus-K52F:~/Escritorio/Ujixy$ git reflog
61daa69 HEAD@0: initial pull

jesus@jesus-K52F:~/Escritorio/Ujixy$ git cat-file -p bfe11a30d57a0233d3b0c840a3b66f6421987304
040000 tree 9196501a346cfe4347f46d82936745b78b0235b9    Catalogoapp
040000 tree 49561b4bd6adb8fe8bb1915d6bef09cd49195a97    Messageapp
040000 tree 0fb58bf9b56397443fb235e2a38045d6df7cd473    Ujixyapp
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    __init__.py
100644 blob dfe3388ddf2d5ba34559eb3ec56199d83cdce8bd    __init__.pyc
100644 blob bcdd55e27be9447bf6b224b8ba0cbc6802509862    manage.py
100644 blob 34c5978d8026844038d530b491828398bc3ea6c7    settings.py
100644 blob 167a6b1965426ec30c25535fe27338b61b2ae0cf    settings.pyc
100644 blob 4a7215cb90ae95d64ca30fde1c1277e0155eb4ed    urls.py
100644 blob 6eedcddafbc8854f70f44181edac8e63781cfb09    urls.pyc

但是,如何恢复包含所有文件和文件夹的目录?现在我正在使用 .git 文件夹的副本以避免其他问题。

【问题讨论】:

【参考方案1】:

由于您已经引用了一个悬垂的树对象,因此您一切顺利。以下应该可以工作:首先将悬空树恢复到 Git 的索引中:

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

接下来,从现在恢复的索引更新您的工作目录:

git checkout-index -a

【讨论】:

拯救了我的一天。谢谢。 哥们也一样...干杯!【参考方案2】:

既然你已经能够在悬空的树对象上运行git cat-file -p,你应该能够恢复它。有很多方法,我将描述2个我能很快想到的:

创建一个新的提交以引入悬空树中的文件。此提交将没有父级。

echo "A commit to recover the dangling tree." | git commit-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

# Output:
<SOME_NEWLY_CREATED_COMMIT_SHA1>

新的提交应该包含你刚刚发现的悬空树的工作树。上述命令的输出应显示已创建的新提交 SHA1。

要将您当前的工作树切换到此提交:

git checkout <SOME_NEWLY_CREATED_COMMIT_SHA1>

现在您应该能够看到悬挂树提交中列出的所有文件和内容。你可以四处浏览,备份文件,做任何你想做的事;)

替代方法:

如果您只想在当前提交的基础上进行更改,这种方法可能很有用。

将树的内容读入 git 的索引(即本例的暂存区)。

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

现在在当前签出分支顶部的暂存区提交更改:

git commit -m "Recover the lost files."

对于未来:

始终提交您的更改,即使提交在将来变得悬空,也更容易获得它们(使用 reflogs)。如果有疑问,请继续使用git commit,您可以随时修改提交、进行更改、重写历史记录等。尤其是在运行git pullgit push 等命令之前,您应该提交您的更改,以便它们不会迷路了。

不要在存储库上运行两次git init,尽管git 足够聪明,可以知道存储库已经初始化并尝试不覆盖您的更改。

【讨论】:

【参考方案3】:

如果您一开始没有提交这些文件和文件夹,我认为您无法恢复它们。 Git 可以恢复您已提交到 repo 中的任何内容,但如果您一开始没有提交,那么它根本不在 repo 中。这也是我喜欢在 Dropbox 文件夹中使用 git 的部分原因。

【讨论】:

以上是关于git删除了所有内容,如何恢复文件和文件夹的主要内容,如果未能解决你的问题,请参考以下文章

git删除之后怎么恢复

从git文件删除后如何恢复文件?

Linux 文件夹的所有内容被误删除恢复?

Git:恢复一个已经在提交中被删除的文件

git 恢复一个被删除的文件,本地已经删除很久了,而且不知道commit_id ,只记得文件名

git批量恢复所有删除的文件