簡體   English   中英

git刪除了所有內容,如何恢復文件和文件夾

[英]git deleted everything, how to recover files and folders

這是我第一次使用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 的索引中:

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>

    新提交應該包含您剛剛發現的懸垂樹的工作樹。 上述命令的輸出應顯示創建的新提交 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足夠聰明,知道存儲庫已經初始化並嘗試不覆蓋您的更改。

我也是 git 新手,我試過了

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304

git checkout-index -a

他們沒有工作,最終我意識到我的樹有不同的字符集。 我使用了: git fsck --lost-found然后我找到了我的樹值,所以我可以應用提到的命令。

如果您一開始沒有提交這些文件和文件夾,我不相信您可以恢復它們。 Git 可以恢復您已提交到存儲庫中的任何內容,但如果您一開始沒有提交,則它根本不在存儲庫中。 這也是我喜歡在 Dropbox 文件夾中使用 git 的部分原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM