Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo a git merge squash?

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.

like image 828
jdias Avatar asked Sep 17 '11 21:09

jdias


People also ask

Can you undo a squash merge?

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.

Can you undo a git merge?

You can undo a Git merge using the git reset –merge command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

How do I undo a merge head?

git merge --abort will abort the merge process and try to reconstruct the pre-merge state. Show activity on this post. Or even HEAD^^ to revert that revert commit. You can always give a full SHA reference if you're not sure how many steps back you should take.


1 Answers

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.

like image 169
Mark Longair Avatar answered Sep 30 '22 03:09

Mark Longair