我不小心在本地主分支上运行了gitmergesome_other_branch。我还没有将更改推送给origin master。如何撤消合并?


合并后,git状态显示:

# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.

我如何撤销所有这些提交?


当前回答

暂存gitbranch-d the_local_branchgit checkout-t<远程名称>应用git存储

这对我有用。。!!

其他回答

好吧,这里的其他人给我的答案很接近,但没用。这是我所做的。

正在执行此操作。。。

git reset --hard HEAD^
git status

…给了我以下状态。

# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.

然后,我不得不多次输入相同的git重置命令。每次我这样做时,信息都会改变一个,如下所示。

> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.

此时,我看到状态消息发生了变化,所以我尝试进行git拉动,这似乎奏效了:

> git pull
Updating 2df6af4..12bbd2f
Fast forward
 app/views/truncated |    9 ++++++---
 app/views/truncated |   13 +++++++++++++
 app/views/truncated |    2 +-
 3 files changed, 20 insertions(+), 4 deletions(-)
> git status
# On branch master

长话短说,我的命令归结为:

git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git pull

最简单的答案是奥迪尼奥-维尔蒙特给出的答案

首先执行git重置--合并ORIG_HEAD

对于那些希望在推送更改后重置的用户,请执行以下操作(因为这是第一篇关于git重置合并问题的文章)

git推原点HEAD—力

这将以这样一种方式重置,即您不会在拉动后再次获得合并的更改。

如果您正在合并过程中,您可以随时中止它

git merge --abort

您可以使用git reflog查找上一次结账。有时候,这是一个你想回到的好状态。

具体而言,

$ git reflog
$ git reset --hard HEAD@{0}

在这种情况下,您需要使用git-reset--hard<branch_name>重置分支。如果要在重新设置之前保存更改,请确保创建一个新的分支并git checkout<branch_name>。

您也可以使用git-reset--hard<commit_id>将状态重置为特定的提交。

如果已推送更改,则可以改用git-restore<branch_name>。请务必了解如何在其他场景中使用git还原和git签出。