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


合并后,git状态显示:

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

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


当前回答

我能够用一个不需要查找提交id的命令解决这个问题。

git reset --hard remotes/origin/HEAD

接受的答案对我不起作用,但这个命令实现了我所期望的结果。

其他回答

对于较新的Git版本,如果您尚未提交合并,并且存在合并冲突,您可以简单地执行以下操作:

git merge --abort

来自man git merge:

只有在合并导致冲突后才能运行[This]。gitmerge--abort将中止合并过程并尝试重建合并前的状态。

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

正在执行此操作。。。

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 merge—中止

最简单的机会,比这里说的任何事情都简单得多:

删除本地分支(本地,而不是远程),然后再次拉动它。这样,您将撤消主分支上的更改,任何人都将受到您不想推送的更改的影响。重新开始。

只需创建新的分支,然后cherry pick所需的提交。

它的保护程序和更简单的重置在上面的许多答案中都有描述