首页 > 解决方案 > 如何在 git pull 之后将本地仓库恢复到上一个​​提交?

问题描述

假设 git pull 命令将我的本地 repo 推进几个提交以匹配远程 repo,并且我希望本地 repo 在 git pull 之前返回到提交 ID。换句话说,我想撤销我从 git pull 得到的一切。我应该使用哪个命令?

标签: gitgithub

解决方案


要切换回分支,只需检查拉取请求输出中列出的 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

推荐阅读