2013-05-07 329 views
6

這是我第一次使用git,我想將一個現有的項目導入到github中,並且所有東西都被刪除了。搜索答案後,我認爲git刪除後,git拉文件,我試圖恢復文件和文件夾,但我找不到如何做到這一點。git刪除了所有東西,如何恢復文件和文件夾

我確實做到了下:

[email protected]:~/Escritorio/Django/Ujixy$ git init 
Initialized empty Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/ 
[email protected]:~/Escritorio/Django/Ujixy$ git add . 
[email protected]:~/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 
[...] 

[email protected]:~/Escritorio/Django/Ujixy$ git push origin master 
fatal: 'origin' does not appear to be a git repository 
fatal: The remote end hung up unexpectedly 
[email protected]:~/Escritorio/Django/Ujixy$ git add * 
[email protected]:~/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 
[...] 
[email protected]:~/Escritorio/Django/Ujixy$ git remote add origin https://github.com/PEREYO/Ujixy.git 
[email protected]:~/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 
[email protected]:~/Escritorio/Django/Ujixy$ git push origin master 
Username for 'https://github.com': PEREYO 
Password for 'https://[email protected]': 
Everything up-to-date 
[email protected]:~/Escritorio/Django/Ujixy$ git init 
Reinitialized existing Git repository in /home/jesus/Escritorio/Django/Ujixy/.git/ 
[email protected]:~/Escritorio/Django/Ujixy$ git add * 
[email protected]:~/Escritorio/Django/Ujixy$ git status 
# On branch master 
nothing to commit (working directory clean) 

現在我試圖修復它做了下:

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

[email protected]:~/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文件夾的副本以避免出現其他問題。

回答

9

由於您已經有了一個懸掛樹木對象的參考,所以您很好。下面應該工作:首先恢復懸空樹到Git的指數:

git read-tree bfe11a30d57a0233d3b0c840a3b66f6421987304 

接下來,從現在恢復的索引更新你的工作目錄:

git checkout-index -a 
+0

非常感謝,它工作。 – PEREYO 2013-05-07 06:23:12

+0

保存了我的一天。謝謝。 – user2085689 2014-03-05 23:39:04

+0

同樣在這裏好友......乾杯! – 2015-07-22 21:38:15

-2

我不相信你可以恢復這些文件和文件夾,如果你還沒有承諾他們擺在首位。 Git可以恢復您在回購協議中承諾的任何內容,但如果您沒有首先承諾,那麼它根本不在回購協議內。這部分是我喜歡在Dropbox文件夾中使用git的原因。

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足夠聰明,可以知道回購已經被初始化並嘗試不覆蓋您的更改。

+0

謝謝,非常有用你的信息和你的建議。 – PEREYO 2013-05-07 06:24:10

相關問題