如何撤消 git pull?

发布于 2024-11-03 12:16:10 字数 89 浏览 4 评论 0原文

由于远程源上不需要的提交,我想撤消 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久光 2024-11-10 12:16:11

或者让它比其他答案更明确:

git pull 

哎呀?

git reset --keep HEAD@{1}

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


致评论者

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

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.


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)

不美如何 2024-11-10 12:16:11

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 the pull. Then you can reset your HEAD to that commit.

千纸鹤带着心事 2024-11-10 12:16:11

这对我有用。

git reset --hard ORIG_HEAD 

撤消合并或拉取:

$ 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)

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

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 Git for more.

幻梦 2024-11-10 12:16:11

找到您想要进行的提交的 。你可以在 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 typing git log or git reflog show at the command line and then do
git reset --hard <SHA#>

不一样的天空 2024-11-10 12:16:11

撤消 git pull

git reset --hard HEAD^

会将本地存储库恢复到之前的提交状态。 (注意:HEAD^ 表示当前分支尖端的第一个直接父级。HEAD^ 是 HEAD^1 的缩写,如果需要,您也可以进一步向上(例如 HEAD^2 、 HEAD^3 ...) 。
重置后,如果留下一些未知文件(由于原始 git pull 到达的文件),它们将被列为未跟踪文件,您可以使用以下命令清理它们

  git clean -fd

to undo git pull

git reset --hard HEAD^

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 clean -fd
萧瑟寒风 2024-11-10 12:16:11

尽管上述解决方案确实有效,但如果您想逆转时钟而不是撤消 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 command

git 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 Branch A but I accidentally ran
git 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"}'

‘画卷フ 2024-11-10 12:16:11

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

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

<前><代码>$ git pull (1)
自动合并硝基酚
通过递归进行合并。
硝石| 20 +++++----
...
$ git reset --merge ORIG_HEAD (2)

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

检查合并结果后,你可能会发现变化
另一个分支的情况则不尽如人意。运行 git reset --hard ORIG_HEAD
会让你回到原来的地方,但它会丢弃
您的本地更改,这是您不想要的。 git reset --merge 保留
您本地的更改。

另请参阅https://stackoverflow.com/a/30345382/621690

From 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 pull when 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_HEAD
will let you go back to where you were, but it will discard
your local changes, which you do not want. git reset --merge keeps
your local changes.

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

帅哥哥的热头脑 2024-11-10 12:16:11

重置master分支:

git reset --hard origin/master

Reset the master branch:

git reset --hard origin/master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文