3 回答

TA貢獻1880條經(jīng)驗 獲得超4個贊
git reflog是你的朋友。在該列表中找到要提交的提交,然后可以將其重置(例如:)git reset --hard e870e41。
(如果您不提交更

TA貢獻1831條經(jīng)驗 獲得超9個贊
首先:是什么HEAD
?
HEAD
只是對當(dāng)前分支中當(dāng)前提交(最新)的引用。任何給定時間
只能有1個HEAD
。
如果您不在最新的提交上,這意味著它HEAD
指向歷史記錄中的先前提交,則稱為分離的HEAD。
幾種選擇:
git checkout
git checkout <commit_id>
git reflog
您可以隨時使用reflog,以及
git reflog
git checkout HEAD@{...}
這將使您回到所需的提交
git reset HEAD --hard <commit_id>
將您的頭“移動”回所需的提交。
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.
注意:(從Git 2.7開始),
您也可以使用git rebase --no-autostash。
git checkout
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits to go back
這將簽出一個指向所需提交的新分支
這是可以完成的一般方案。

TA貢獻1863條經(jīng)驗 獲得超2個贊
獲取已刪除提交的另一種方法是使用git fsck命令。
git fsck --lost-found
這將輸出類似于最后一行的內(nèi)容:
dangling commit xyz
我們可以使用reflog其他答案中的建議來檢查它是否是相同的提交?,F(xiàn)在我們可以做一個git merge
git merge xyz
注:
我們不能讓犯回來fsck,如果我們已經(jīng)運行一個git gc命令,它會刪除提及懸掛承諾。
- 3 回答
- 0 關(guān)注
- 748 瀏覽
添加回答
舉報