Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
git删除了一切,如何恢复文件和文件夹_Git_Github_Git Pull_Recover - Fatal编程技术网

git删除了一切,如何恢复文件和文件夹

git删除了一切,如何恢复文件和文件夹,git,github,git-pull,recover,Git,Github,Git Pull,Recover,这是我第一次使用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:~

这是我第一次使用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文件夹的副本来避免另一个问题。

我认为,如果您没有首先提交这些文件和文件夹,您就无法恢复它们。Git可以恢复您在回购协议中承诺的任何内容,但如果您一开始没有承诺,它就根本不在回购协议中。这也是我喜欢在Dropbox文件夹中使用git的部分原因。

因为您已经有了对悬垂树对象的引用,所以您已经走上了正轨。以下操作应该可以工作:首先将悬空树恢复到Git的索引中:

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304
接下来,从现在恢复的索引更新工作目录:

git checkout-index -a

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

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

    echo "A commit to recover the dangling tree." | git commit-tree bfe11a30d57a0233d3b0c840a3b66f6421987304
    
    # Output:
    <SOME_NEWLY_CREATED_COMMIT_SHA1>
    
    现在在当前签出分支顶部的暂存区域中提交更改:

    git commit -m "Recover the lost files."
    
未来:

  • 始终提交您的更改,即使将来提交会悬而未决,也更容易获得更改(使用reflogs)。如果有疑问,请继续执行
    git提交
    ,您可以随时进行提交、进行更改、重写历史记录等。特别是在运行诸如
    git pull
    git push
    等命令之前,您应该提交更改,以免丢失

  • 不要在存储库上运行两次
    git init
    ,尽管
    git
    非常聪明,知道repo已经初始化,并且尝试不覆盖您的更改


拯救了我的一天。谢谢,我也是,伙计。。。干杯
git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304
git commit -m "Recover the lost files."