[英]How to undo a git merge squash?
I have just done a 我刚做了一个
git merge --squash feature-branch
into my develop
branch. 进入我的
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 develop
branch before and after the merge are exactly the same. 简而言之,合并之前和之后
develop
分支的日志完全相同。
Is there a way to revert back develop
to 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. 根据Dan D.的评论和下面接受的答案,我能够解决我的问题。 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. 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. 2 - 我没有像建议的那样执行
git reset HEAD@{1}
,而是在我最后一次提交开发时找到了我希望保留的数字。 In my case it was HEAD@{188}
. 就我而言,它是
HEAD@{188}
。 So I typed git reset HEAD@{188}
. 所以我输入了
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. 3 - 我运行了一个
git log
,它有一个干净的日志,只显示我在进行错误合并之前的提交。
4 - I ran git add -A .
4 - 我运行了
git add -A .
to stage all the new files created during my feature development. 暂存在功能开发期间创建的所有新文件。
5 - I ran git commit -m "install feature-x"
5 - 我运行
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
. 6 - 结果现在我使用正确的文件进行分支
develop
并且日志是干净的 - 仅显示我在开发feature-x
期间所做的所有更改的一次提交。
I still need to find out why my original git merge --squash feature-branch
did not work as intended. 我仍然需要找出为什么我原来的
git merge --squash feature-branch
没有按预期工作。
Solution 2 解决方案2
Mark Longair's answer is a definitive solution to my problem. Mark Longair的答案是解决我的问题的最终解决方案。 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: 请参阅下面我正在使用的流程来压缩
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. 上面的序列就像一个魅力。
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. 如果你运行
git merge --squash <other-branch>
,工作树和索引会更新合并的结果,但它不会创建提交。 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. 您不需要使用reflog。
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 --abort
is only equivalent to git reset --merge
given that MERGE_HEAD
is present. 但实际上,值得注意的是
git merge --abort
只相当于git reset --merge
因为存在MERGE_HEAD
。 This can be read in the git help for merge command. 这可以在git help for merge命令中读取。
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 . 合并失败后,当没有
MERGE_HEAD
,可以使用git reset --merge
撤消失败的合并,但不一定使用git merge --abort
, 因此它们不仅是旧的和新的语法相同的东西 。 Personally I find git reset --merge
much more useful in everyday work. 就个人而言,我发现
git reset --merge
在日常工作中更有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.