簡體   English   中英

git pull 后如何將本地 repo 恢復為之前的提交?

[英]How to revert the local repo back to the previous commit after git pull?

假設 git pull 命令將我的本地 repo 推進幾個提交以匹配遠程 repo,並且我希望本地 repo 回到 git pull 之前的提交 ID。 換句話說,我想撤消從 git 拉取的所有內容。 我應該使用哪個命令?

要切換回分支,只需檢查拉取請求的 output 中列出的 Git 提交:

$ git pull
Updating d5c40ea5e..29f1ae2b3
Fast-forward
 README.md                                             | 15 +++++++++++++++
 gradle.properties                                     |  2 +-
 2 files changed, 16 insertions(+), 1 deletions(-)

請注意此更新的d5c40ea5e 只需檢查這個分支,代碼就會回到拉取請求之前的狀態。

$ git checkout d5c40ea5e
Note: switching to 'd5c40ea5e'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d5c40ea5e Update code to be super awesome

我不知道有什么靈丹妙葯的方法,但是通過一些小的偵探工作並不難。 首先從有問題的本地分支運行git log 您可能會在分支的 HEAD 處看到合並提交。 如果您的拉取策略是合並,就會出現這種情況。 或者,如果您的分支被快進,您可能會看到一些新的提交。 無論哪種情況,只需在 pull 的內容之前找到提交的 SHA-1 hash,然后硬重置為它:

# from your local branch
git reset --hard <SHA-1 of earlier commit>

請注意,如果您的 Git 拉取策略是變基,那么您還有另一個問題。 現在您不能簡單地重置為較早的提交,因為拉動可能已經改寫了您的歷史。 在這種情況下清理可能需要更多的工作。

暫無
暫無

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

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