3 回答

TA貢獻1963條經(jīng)驗 獲得超6個贊
這很大程度上取決于“恢復”的含義。
暫時切換到其他提交
如果你想暫時回到它,傻瓜,然后回到你所在的位置,你所要做的就是檢查所需的提交:
# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32
或者,如果你想在那里做提交,那么在你做的時候繼續(xù)做一個新的分支:
git checkout -b old-state 0d1d7fc32
要回到原來的位置,只需查看您再次訪問的分支機構(gòu)。(如果你做了更改,就像轉(zhuǎn)換分支一樣,你必須在適當?shù)臅r候處理它們。你可以重置它們?nèi)拥羲鼈?你可以藏匿,結(jié)賬,存放pop以帶走它們;你可以提交如果你想在那里有一個分支,他們到那里的一個分支。)
硬刪除未發(fā)布的提交
另一方面,如果你想真正擺脫自那時以來所做的一切,那么有兩種可能性。一,如果您尚未發(fā)布任何這些提交,只需重置:
# 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.
如果你搞砸了,你已經(jīng)拋棄了你當?shù)氐淖兓?,但你至少可以通過再次重置來回到原來的位置。
使用新提交撤消已發(fā)布的提交
另一方面,如果您已發(fā)布作品,則可能不希望重置分支,因為這有效地重寫了歷史記錄。在這種情況下,您確實可以還原提交。使用Git,revert有一個非常具體的含義:使用反向補丁創(chuàng)建一個提交以取消它。這樣您就不會重寫任何歷史記錄。
# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053
# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD
#Similarly, you can revert a range of commits using commit hashes:
git revert a867b4af..0766c053
# Reverting a merge commit
git revert -m 1 <merge_commit_sha>
# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .
# Then commit. Be sure and write a good message describing what you just did
git commit
該git-revert
手冊頁實際上涵蓋在其描述中有很多這一點。另一個有用的鏈接是這個討論git-revert的git-scm.com部分。
如果您決定不想還原,則可以還原還原(如此處所述)或重置為還原之前(請參閱上一節(jié))。
在這種情況下,您可能還會發(fā)現(xiàn)此答案很有用:
如何將HEAD移回以前的位置?(獨立頭)

TA貢獻1780條經(jīng)驗 獲得超1個贊
將工作副本還原為最近的提交
要恢復到先前的提交,請忽略任何更改:
git reset --hard HEAD
其中HEAD是當前分支中的最后一次提交
將工作副本還原為較舊的提交
要恢復到比最近提交更早的提交:
# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
# Updates working copy to reflect the new commit
git reset --hard
Credits轉(zhuǎn)到類似的Stack Overflow問題,在Git中恢復為SHA哈希的提交?。

TA貢獻1805條經(jīng)驗 獲得超10個贊
我和其他人的最佳選擇是Git重置選項:
git reset --hard <commidId> && git clean -f
這對我來說是最好的選擇!它簡單,快速,有效!
注意: 如果您與擁有舊提交副本的其他人共享您的分支,則如評論中所述,請勿執(zhí)行此操作
同樣來自評論,如果你想要一個較少的'ballzy'方法,你可以使用
git clean -i
- 3 回答
- 0 關注
- 865 瀏覽
添加回答
舉報