如何撤消 git 合并壁球?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7457942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:01:18  来源:igfitidea点击:

How to undo a git merge squash?

gitversion-controlgit-merge

提问by jdias

I have just done a

我刚刚做了一个

git merge --squash feature-branch

into my developbranch.

进入我的develop分支。

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.

问题是上面的命令在没有创建新提交的情况下更新了头部。我的目的是创建一个单一的提交来应用到develop.

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

所以简而言之,develop合并前后分支的日志完全相同。

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

有没有办法恢复develop到它之前的样子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:

根据 Dan D. 的评论和下面接受的答案,我能够解决我的问题。看看下面我做了什么,以防你在同一条船上:

1 - I ran git reflogand it listed all the commits and checkouts I did with my developbranch.

1 - 我跑了git reflog,它列出了我对我的develop分支所做的所有提交和检出。

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}.

2 -git reset HEAD@{1}我没有按照建议的那样做,而是在我最后一次提交开发时找到了我想保留的数字。就我而言,它是HEAD@{188}. 所以我输入了git reset HEAD@{188}.

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

3 - 我运行了一个git log,它有一个干净的日志,只显示我在错误合并之前的提交。

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

4 - 我跑去git add -A .暂存在我的功能开发过程中创建的所有新文件。

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

5 - 我跑了 git commit -m "install feature-x"

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

6 - 结果现在我有develop正确文件的分支并且日志是干净的 - 只显示我在feature-x.

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

我仍然需要找出为什么我的原件git merge --squash feature-branch没有按预期工作。

Solution 2

解决方案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-branchand include just one commit to the developbranch:

Mark Longair 的回答是我问题的最终解决方案。我刚刚测试了它并且它有效。请参阅下面我现在用来压缩 a 中的所有内部提交feature-branch并仅包含一个对develop分支的提交的过程:

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

The above sequence works like a charm.

上面的序列就像一个魅力。

回答by Mark Longair

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 merge --squash <other-branch>工作树,并且索引会更新为合并的结果,但它不会创建提交。您需要做的就是运行:

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.

您不需要使用引用日志。

回答by Martin G

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

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

Abort the merge with modern git syntax:

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

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 --abortis only equivalent to git reset --mergegiven that MERGE_HEADis present. This can be read in the git help for merge command.

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

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 --mergebut not necessarily with git merge --abort, so they are not only old and new syntax for the same thing. Personally I find git reset --mergemuch more useful in everyday work.

合并失败后,如果没有MERGE_HEAD,失败的合并可以用 撤消,git reset --merge但不一定用git merge --abort因此它们不仅是同一事物的新旧语法。我个人觉得git reset --merge在日常工作中更有用。