撤消尚未推送的 Git 合并 [英] Undo a Git merge that hasn't been pushed yet

查看:31
本文介绍了撤消尚未推送的 Git 合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 master 分支中,我在本地做了一个 git merge some-other-branch,但从未将更改推送到 origin master.我不是想合并,所以我想撤消它.在合并后执行 git status 时,我收到此消息:

# 在分支 master# 你的分支比 'origin/master' 领先 4 次提交.

基于一些我发现的说明,我试过跑步

git revert HEAD -m 1

但现在我收到了带有 git status 的消息:

# 在分支 master# 你的分支比 'origin/master' 领先 5 次提交.

我不希望我的分支领先于任何数量的提交.我如何回到那个点?

解决方案

使用 git reflog 检查哪个提交是合并前的提交(git reflog 将是更好的选择比 git log).然后您可以使用以下方法重置它:

git reset --hard commit_sha

还有一种方法:

git reset --hard HEAD~1

它会让你返回 1 个提交.

请注意,任何已修改和未提交/未隐藏的文件都将重置为其未修改状态.为了让它们隐藏更改或查看下面的 --merge 选项.

<小时>

正如@Velmont 在下面的回答中所建议的,在这种直接的情况下使用:

git reset --hard ORIG_HEAD

可能会产生更好的结果,因为它应该保留您的更改.ORIG_HEAD 将在合并发生之前直接指向提交,因此您不必自己寻找.

<小时>

另一个提示是使用 --merge 开关而不是 --hard 因为它不会不必要地重置文件:

git reset --merge ORIG_HEAD

<块引用>

--合并

重置索引并更新工作树中与<commit>不同的文件.和 HEAD,但保留索引和工作树之间不同的那些(即具有尚未添加的更改).

Within my master branch, I did a git merge some-other-branch locally, but never pushed the changes to origin master. I didn't mean to merge, so I'd like to undo it. When doing a git status after my merge, I was getting this message:

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

Based upon some instructions I found, I tried running

git revert HEAD -m 1

but now I'm getting this message with git status:

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

I don't want my branch to be ahead by any number of commits. How do I get back to that point?

解决方案

With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using:

git reset --hard commit_sha

There's also another way:

git reset --hard HEAD~1

It will get you back 1 commit.

Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state. To keep them either stash changes away or see --merge option below.


As @Velmont suggested below in his answer, in this direct case using:

git reset --hard ORIG_HEAD

might yield better results, as it should preserve your changes. ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it yourself.


A further tip is to use the --merge switch instead of --hard since it doesn't reset files unnecessarily:

git reset --merge ORIG_HEAD

--merge

Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).

这篇关于撤消尚未推送的 Git 合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆