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


合并后,git状态显示:

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

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


当前回答

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

正在执行此操作。。。

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 --hard origin/<branch-name>

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

我认为您可以执行gitrebase-I[hash][branch_name],其中[hash]是标识哈希值,表示您希望倒回的位置加上一个(或您希望返回的提交次数),然后在编辑器中删除您不再需要的提交行。保存文件。出口祈祷它应该重新缠绕。您可能需要进行一次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重置--硬@{u}

注意:@{u}是origin/master的简写。(当然,您需要远程存储库才能实现这一点。)

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

  git reset --hard HEAD~1

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

git merge --abort

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

 git reset --hard <commit-id>