如何撤消GIT合并南瓜?

发布于 2024-12-05 06:42:37 字数 1302 浏览 0 评论 0原文

我刚刚完成了

git merge --squash feature-branch

开发分支。

问题在于,上面的命令在没有创建新提交的情况下更新了头部。我的目的是创建一个单一提交,以应用于开发的头部。

因此,简而言之,合并之前和之后的开发分支的日志完全相同。

有没有一种方法可以将恢复到git Merge之前开发

谢谢。

解决方案

基于Dan D.的评论以及下面接受的答案,我能够解决我的问题。请参阅下面我所做的事情,以防您在同一条船上:

1-我运行git reflog,并列出了我对我develop分支所做的所有提交和结帐。

2-我没有按建议进行git重置头@{1},而是找到了我想保留的最后一个提交时的数字。就我而言,它是head@{188}。因此,我键入git重置头@{188}

3-我运行了git日志,它的日志清洁,仅显示我进行错误合并之前的提交。

4-我运行git add -a。登台我在功能开发过程中创建的所有新文件。

5-我运行git commit -m“ install feature -x”

6-结果,我现在有了branch 使用正确的文件开发,日志已清洁 - 仅显示一个提交我在开发feature-x过程中所做的所有更改。

我仍然需要找出为什么我的原始git合并-squash feature-branch无法按预期工作。

解决方案2

Mark Longair的答案是解决我问题的确定解决方案。我刚刚测试了它,它有效。请参阅下面的过程,我现在正在使用feature-branch中的所有内部提交,并仅包含一个提交开发分支:

git checkout develop
git merge --squash feature-branch
git commit -m "install of feature-branch"

上面的序列就像魅力一样工作。

I have just done a

git merge --squash feature-branch

into my develop branch.

The problem is that the above command updated the head without creating a new commit. My intention was to create one single commit to apply to the head of develop.

So in short, the log for the develop branch before and after the merge are exactly the same.

Is there a way to revert back develop to what it was before the git merge?

Thank you.

Solution

Based on the comment from Dan D. and the accepted answer below I was able to resolve my problem. See below what I did in case you are in the same boat:

1 - I ran git reflog and it listed all the commits and checkouts I did with my develop branch.

2 - Instead of doing a git reset HEAD@{1} as suggested, I found the number when I did the last commit to develop that I wanted to keep. In my case it was HEAD@{188}. So I typed git reset HEAD@{188}.

3 - I ran a git log and it had a clean log showing only the commits I had before I did the wrong merge.

4 - I ran git add -A . to stage all the new files created during my feature development.

5 - I ran git commit -m "install feature-x"

6 - As a result now I have branch develop with the correct files and the log is clean - showing only one commit for all the changes I did during the development of feature-x.

I still need to find out why my original git merge --squash feature-branch did not work as intended.

Solution 2

Mark Longair's answer is a definitive solution to my problem. I have just tested it and it works. See below the process I am using now to squash all the internal commits within a feature-branch and include just one commit to the develop branch:

git checkout develop
git merge --squash feature-branch
git commit -m "install of feature-branch"

The above sequence works like a charm.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2024-12-12 06:42:37

如果您运行 git merge --squash工作树和索引将根据合并结果进行更新,但不会创建提交。您需要做的就是运行:

git commit

但是,如果您在提交之前改变主意并且只想中止合并,则只需运行:

git reset --merge

您不需要使用引用日志。

If you run git merge --squash <other-branch> the working tree and index are updated with what the result of the merge would be, but it doesn't create the commit. All you need to do is to run:

git commit

However, if you change your mind before committing and just want to abort the merge, you can simply run:

git reset --merge

You don't need to use the reflog.

不及他 2024-12-12 06:42:37

如果您在提交之前改变主意,您有以下选择:

使用现代 git 语法中止合并:

git merge --abort

使用旧语法:

git reset --merge

非常老式:

git reset --hard

但实际上,值得注意的是 git merge --abort 仅在存在 MERGE_HEAD 的情况下相当于 git reset --merge。这可以在 git 合并命令帮助中阅读。

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

合并失败后,当没有 MERGE_HEAD 时,可以使用 git reset --merge 撤消失败的合并,但不一定使用 git merge --abort< /code>,所以它们不仅仅是同一事物的新旧语法。就我个人而言,我发现 git reset --merge 在日常工作中更有用。

If you change your mind before committing, you have these options:

Abort the merge with modern git syntax:

git merge --abort

And with older syntax:

git reset --merge

And really old-school:

git reset --hard

But actually, it is worth noticing that git merge --abort is only equivalent to git reset --merge given that MERGE_HEAD is present. This can be read in the git help for merge command.

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --merge but not necessarily with git merge --abort, so they are not only old and new syntax for the same thing. Personally I find git reset --merge much more useful in everyday work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文