How to undo a git merge squash?

允我心安 提交于 2020-01-01 04:16:07

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7457942/how-to-undo-a-git-merge-squash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!