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


合并后,git状态显示:

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

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


当前回答

您只能使用两个命令通过特定提交还原合并或重新启动:

git reset--硬commitHash(您应该使用要重新启动的commit,例如44a587491e32eafa163ca7738)git push origin HEAD--force(将新的本地主分支发送到origin/master)

祝你好运,加油!

其他回答

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

正在执行此操作。。。

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 reset命令。

git reset-将当前HEAD重置为指定状态。git重置[--混合|--soft|--hard|--merge][-q][]git-reset[-q][][-]…git重置--补丁[] [--] […]

GIT重置

假设您的本地主机不领先于源/主机,您应该能够

git reset --hard origin/<branch-name>

因此,假设您在master上执行了此操作,那么您的本地master分支看起来应该与origin/master相同。

如果您的合并和相应的提交尚未被推送,您可以始终切换到另一个分支,删除原始分支并重新创建它。

例如,我意外地将一个开发分支合并到master中,并想撤消它。使用以下步骤:

git checkout develop
git branch -D master
git branch -t master origin/master

瞧!Master与原点处于同一阶段,您的错误合并状态将被清除。

如果没有冲突和合并完成,则:

  git reset --hard HEAD~1

如果在执行合并时发生冲突,则中止将使您退出最近的合并更改:

git merge --abort

或者如果您想恢复到特定的提交id。

 git reset --hard <commit-id>