Git删除了所有文件,如何恢复文件和文件夹

11

这是我第一次使用git,我想将一个现有项目导入github,结果所有文件都被删除了。经过搜索,我认为是在进行git pull之后git删除了这些文件。我正在尝试恢复这些文件和文件夹,但我找不到如何操作的方法。

我的操作如下:

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 文件夹的一个副本来避免其他问题。

4个回答

20

既然你已经拥有了一个指向悬空树对象的引用,你已经做得很好了。接下来应该这样操作:首先将悬空树恢复到Git的索引中:

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

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

git checkout-index -a

2

既然您能够在悬空的树对象上运行git cat-file -p,那么您应该能够恢复它。有许多方法可以实现这一点,我将描述我能够迅速想到的两种方法:

  • Create a new commit to bring-in the files in the dangling tree. This commit will have no parent.

    echo "A commit to recover the dangling tree." | git commit-tree bfe11a30d57a0233d3b0c840a3b66f6421987304
    
    # Output:
    <SOME_NEWLY_CREATED_COMMIT_SHA1>
    

    The new commit should contain the work-tree of the dangling tree that you just found out. The output of the above command should show the new commit SHA1 which was created.

    To switch your current work-tree to this commit:

    git checkout <SOME_NEWLY_CREATED_COMMIT_SHA1>
    

    Now you should be able to see all the files and the contents that were listed in the dangling tree commit. You can browse around, make a backup of the files, do whatever you want ;)

  • Alternative approach:

    If you would like to just get the changes on top of your current commit, this approach might be useful.

    Read the contents of the tree into git's index (i.e. into the staging area for this case).

    git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304
    

    Now commit the changes in the staging area on top of the currently checked out branch:

    git commit -m "Recover the lost files."
    

未来需要注意的事项:

  • 始终提交你的更改,即使提交被悬置在未来,也可以使用引用日志轻松找到它们。如果不确定,请使用 git commit 命令,你可以随时修正提交、进行更改、重写历史等操作。特别是在运行 git pullgit push 等命令之前,应该提交你的更改,以防丢失。

  • 不要在一个仓库上两次运行 git init 命令,尽管 git 足够智能,知道该仓库已经初始化,并且试图不覆盖你的更改。


0

我也是 Git 的新手,我尝试过

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

并且

git checkout-index -a

它们没有起作用,最终我意识到我的树具有不同的字符集。我使用了:

git fsck --lost-found

然后我找到了我的树值,所以我可以应用提到的命令。

-1

如果你一开始没有提交这些文件和文件夹,我不相信你能够恢复它们。Git 可以恢复你已经提交到仓库中的任何内容,但如果你一开始没有提交,它根本不在仓库中。这也是我喜欢在 Dropbox 文件夹中使用 Git 的部分原因。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接