如何撤消 git pull?
由于远程源上不需要的提交,我想撤消 git pull ,但我不知道必须重置回哪个版本。
我怎样才能回到在远程源上执行 git pull 之前的状态?
I would like to undo my git pull on account of unwanted commits on the remote origin, but I don't know to which revision I have to reset back to.
How can I just go back to the state before I did the git pull on the remote origin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
或者让它比其他答案更明确:
哎呀?
1.7.1 之前的 git 版本没有
--keep
。如果您使用这样的版本,您可以使用--hard
- 但这是一个危险的操作,因为它会丢失任何本地更改。致评论者
Or to make it more explicit than the other answer:
whoops?
Versions of git older than 1.7.1 do not have
--keep
. If you use such version, you could use--hard
- but that is a dangerous operation because it loses any local changes.To the commenter
git reflog show
应该显示 HEAD 的历史记录。您可以使用它来确定您在拉动
之前所处的位置。然后您可以重置
您的HEAD
到该提交。git reflog show
should show you the history of HEAD. You can use that to figure out where you were before thepull
. Then you canreset
yourHEAD
to that commit.这对我有用。
撤消合并或拉取:
查看此:Git 中的 HEAD 和 ORIG_HEAD 了解更多信息。
This worked for me.
Undo a merge or pull:
Checkout this: HEAD and ORIG_HEAD in Git for more.
找到您想要进行的提交的
。你可以在 github 中找到它,或者在命令行输入 git log 或 git reflog show ,然后执行git reset --hard
Find the
<SHA#>
for the commit you want to go. You can find it in github or by typinggit log
orgit reflog show
at the command line and then dogit reset --hard <SHA#>
撤消 git pull
会将本地存储库恢复到之前的提交状态。 (注意:HEAD^ 表示当前分支尖端的第一个直接父级。HEAD^ 是 HEAD^1 的缩写,如果需要,您也可以进一步向上(例如 HEAD^2 、 HEAD^3 ...) 。
重置后,如果留下一些未知文件(由于原始 git pull 到达的文件),它们将被列为未跟踪文件,您可以使用以下命令清理它们
to undo git pull
takes your local repo back to previous commit state. (Note: HEAD^ means the first immediate parent of the tip of the current branch. HEAD^ is short for HEAD^1, and you can also go further up if you want (e.g. HEAD^2 , HEAD^3 ...).
after reset, if some unknown files left (files that arrived as a result of original git pull), they will be listed as untracked files and you can clean them up using
尽管上述解决方案确实有效,但如果您想逆转时钟而不是撤消 git pull,则此答案适合您。我的意思是,如果您想以
X
分钟的方式获取确切的存储库,请运行此命令git reset --hardbranchName@{"X Minutes ago"}
注意:在您实际继续运行此命令之前,请仅在您确定要返回的时间以及我的情况时才尝试此命令。
我目前在一个分支
develop
上,我应该签出到一个新分支并拉入另一个分支,比如说分支A
但我不小心跑了在签出之前
git pull origin A
。因此,为了撤消此更改,
如果
您使用 Windows cmd 并获取 错误:未知开关 `e
尝试添加这样的引号
git reset --hard 'develop@{"10 分钟之前"}'
Even though the above solutions do work, This answer is for you in case you want to reverse the clock instead of undoing a git pull. I mean if you want to get your exact repo the way it was
X
Mins back then run this commandgit reset --hard branchName@{"X Minutes ago"}
Note: before you actually go ahead and run this command please only try this command if you are sure about the time you want to go back to and heres about my situation.
I was currently on a branch
develop
, I was supposed to checkout to a new branch and pull in another branch lets say BranchA
but I accidentally rangit pull origin A
before checking out.so to undo this change I tried this command
git reset --hard develop@{"10 Minutes ago"}
if you are on windows cmd and get error: unknown switch `e
try adding quotes like this
git reset --hard 'develop@{"10 Minutes ago"}'
来自 https://git-scm.com/docs /git-reset#Documentation/git-reset.txt-Undoamergeorpullinsideadirtyworkingtree
另请参阅https://stackoverflow.com/a/30345382/621690
From https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-Undoamergeorpullinsideadirtyworkingtree
See also https://stackoverflow.com/a/30345382/621690
重置master分支:
Reset the master branch: