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

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

问题描述

在我的主分支中,我在本地执行了 git merge some-other-branch ,但从未将更改推送到原始主控。我不是故意要合并,所以我想撤消它。在合并之后执行 git status 时,我收到了以下消息:

 #分支大师
#你的分支在4次提交之前领先于'origin / master'。

基于一些说明我找到了,我试过了

  git revert HEAD -m 1 

但现在我收到了 git status

 #关于分支主
#你的分支在5次提交之前领先于'origin / master'。

我不希望我的分支先于任何提交数量。

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

  git reset --hard commit_sha 

还有另外一种方法

  git reset --hard HEAD 〜1 

会让您回复1次提交。



请注意,任何已修改和未提交/未分散的文件都将重置为未修改状态。要让它们保持变化或看到下面的 - merge 选项。




正如@Velmont在他的回答中提出的,在这个直接案例中使用:

  git reset --hard ORIG_HEAD 

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



< hr>

另一个技巧是使用 - merge 开关而不是 - hard code>,因为它不会不必要地重置文件:


- 合并

重置索引并更新工作树中不同于< 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 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

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:

--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天全站免登陆