如何撤消 git pull?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5815448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:48:43  来源:igfitidea点击:

How to undo a git pull?

gitundopull

提问by Kartins

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.

由于远程源上不需要提交,我想撤消我的 git pull,但我不知道我必须重置回哪个修订版。

How can I just go back to the state before I did the git pull on the remote origin?

我怎样才能回到我在远程源上执行 git pull 之前的状态?

回答by sehe

Or to make it more explicit than the other answer:

或者使其比其他答案更明确:

git pull 

whoops?

哎呀?

git reset --keep HEAD@{1}

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.

早于 1.7.1 的 git 版本没有--keep. 如果你使用这样的版本,你可以使用--hard- 但这是一个危险的操作,因为它会丢失任何本地更改。



To the commenter

给评论者

ORIG_HEAD is previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them. It is less useful now that Git has reflog: HEAD@{1} is roughly equivalent to ORIG_HEAD (HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation)

ORIG_HEAD 是 HEAD 的先前状态,由可能具有危险行为的命令设置,以便于恢复它们。现在 Git 有 reflog 用处不大:HEAD@{1} 大致相当于 ORIG_HEAD(HEAD@{1} 总是 HEAD 的最后一个值,ORIG_HEAD 是危险操作前 HEAD 的最后一个值)

回答by Noufal Ibrahim

git reflog showshould show you the history of HEAD. You can use that to figure out where you were before the pull. Then you can resetyour HEADto that commit.

git reflog show应该向您展示 HEAD 的历史。您可以使用它来确定您在pull. 然后你可以resetHEAD的那个提交。

回答by Parag Tyagi -morpheus-

This worked for me.

这对我有用。

git reset --hard ORIG_HEAD 

Undo a merge or pull:

撤消合并或拉取:

$ git pull                         (1)
Auto-merging nitfol
CONFLICT (content): Merge conflict in nitfol
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard                 (2)
$ git pull . topic/branch          (3)
Updating from 41223... to 13134...
Fast-forward
$ git reset --hard ORIG_HEAD       (4)

Checkout this: HEAD and ORIG_HEAD in Gitfor more.

查看此:Git 中的 HEAD 和 ORIG_HEAD了解更多信息。

回答by Nina

Find the <SHA#>for the commit you want to go. You can find it in github or by typing git logor git reflog showat the command line and then do git reset --hard <SHA#>

找到<SHA#>你想要去的提交。您可以在 github 中或通过键入git loggit reflog show在命令行中找到它,然后执行 git reset --hard <SHA#>

回答by Risadinha

From https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-Undoamergeorpullinsideadirtyworkingtree

来自https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-Undoamergeorpullinsideadirtyworkingtree

Undo a merge or pull inside a dirty working tree

$ git pull           (1)
Auto-merging nitfol
Merge made by recursive.
 nitfol               |   20 +++++----
 ...
$ git reset --merge ORIG_HEAD      (2)

Even if you may have local modifications in your working tree, you can safely say git pullwhen you know that the change in the other branch does not overlap with them.

After inspecting the result of the merge, you may find that the change in the other branch is unsatisfactory. Running git reset --hard ORIG_HEADwill let you go back to where you were, but it will discard your local changes, which you do not want. git reset --mergekeeps your local changes.

撤消合并或拉入肮脏的工作树

$ git pull           (1)
Auto-merging nitfol
Merge made by recursive.
 nitfol               |   20 +++++----
 ...
$ git reset --merge ORIG_HEAD      (2)

即使您的工作树中可能有本地修改,git pull当您知道另一个分支中的更改与它们不重叠时,您可以放心地说。

检查合并的结果后,您可能会发现其他分支的更改并不令人满意。运行git reset --hard ORIG_HEAD将让您回到原来的位置,但它会丢弃您不想要的本地更改。git reset --merge保持您的本地更改。

See also https://stackoverflow.com/a/30345382/621690

另见https://stackoverflow.com/a/30345382/621690