2013-11-02 43 views
3

有點混帳新手,我想確保我沒有對我的git倉庫造成任何大的損害。基本上,我用git reset --hard HEAD恢復到我以前的提交,因爲我搞砸了一些事情。然後我意識到我不想失去我所做的一切,所以我做了一個git reset [email protected]{1}。這似乎沒有任何幫助。然後我試了一個git reset --hard ORIG_HEAD我真的搞亂了我的倉庫嗎?

所以我想,無論如何,我會做一點點的工作。我做了後,我去推它,我不斷收到此錯誤:

Updates were rejected because the tip of your current branch is behind

我試圖git pull origin和它一直不允許它一堆理由我不能完全記住。

最後,我最終做了一個git push -f,它似乎工作正常,我的項目再次運作良好。

這裏是我的git reflog輸出,如果有幫助:

enter image description here

我只是好奇,如果我以某種方式真的搞砸了,我被推到無論是我的到位桶倉庫或我的地方之一是能夠條款再次恢復。有沒有辦法檢查我的git倉庫是否仍然正常運行?

編輯:到位桶也說我「剝離」提交...

回答

7

這將是確定

放鬆。

爲了緩解您的擔憂,請在另一個目錄中克隆另一個副本。然後檢查一下,然後輸入要推送的新代碼。它將全部解決。

確認!一個提交是懸而未決的!

根據您的文本,我認爲您可能已經重寫了主人的提交歷史記錄/樹,結果爲git reset [email protected]{1}後跟git reset --hard ORIG_HEAD。完全按照你所說的行事,我也「失去」了一個承諾。

這可能是您看到的錯誤消息。

20:19:44 (master) ~/code/wat/so_test$ git push 
To [email protected]:rgbkrk/so_test.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to '[email protected]:rgbkrk/so_test.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Integrate the remote changes (e.g. 
hint: 'git pull ...') before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

強制git推動導致主分支走你的新路徑。

還記得我說過放鬆嗎?那麼,這個承諾並沒有消失。假設你有原來的回購工作,它應該仍然在git reflog,但不在git log。它只是在那裏晃來晃去。

2b47149 [email protected]{2}: commit: The bad commit 
01346b9 [email protected]{3}: reset: moving to ORIG_HEAD 
42af608 [email protected]{4}: reset: moving to [email protected]{3} 
01346b9 [email protected]{5}: reset: moving to [email protected]{1} 
2f2d2ce [email protected]{6}: commit: More junk text 
01346b9 [email protected]{7}: commit: Text 
42af608 [email protected]{8}: clone: from [email protected]:rgbkrk/so_test.git 

如果您確實需要提交回來,您可以對該提交和當前主服務器的HEAD進行比較。 git diff 2f2d2ce..HEAD在我爲此問題創建的回購案例中。您始終可以將它合併到主人。

找到「無法訪問」提交的簡單方法是使用git fsck,確保啓用--no-reflogs

20:48:02 (master) ~/code/wat/so_test$ git fsck --unreachable --no-reflogs 
Checking object directories: 100% (256/256), done. 
Checking objects: 100% (5/5), done. 
unreachable tree 0ca2f12b74de7a33f6dc62374a85d982a96531ce 
unreachable commit 2f2d2ce1b6591f0e7a5f67af81a125231ee2e9a1 
unreachable blob bcb7bae71bbdfb79ba31ee41e4aa3a11b1ad2f7f 

有關恢復丟失提交的更多信息,請查看git ready's article

注爲未來

幾件事情要指出:git reset --hard可能是危險的,因爲它扔掉所有提交的更改。如果您有一些工作要保留,但不希望在此提交中使用,請使用git stash

+0

那麼,我的BitBucket回購確實有我想要推的最新代碼,當我克隆它時,我可以看起來像'git reset'到任何我想要的點,所以我應該看起來很好,對不對? –

+0

現在,是的。我唯一擔心的是,如果實際上在主人背後提交或兩次提交,您可能已經完成了一次強制推送。 –

+0

感謝所有這些信息,它真的把它清除了。所以基本上,除非我需要一個小小的提交,不用擔心? –