How to undo a git merge squash?

27,796

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.

Share:
27,796

Related videos on Youtube

jdias
Author by

jdias

Updated on July 09, 2022

Comments

  • jdias
    jdias almost 2 years

    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.

  • jdias
    jdias over 12 years
    Uau, thank you. I am working in a new feature branch and I will test your suggestion to see if it works. I will update this question with the results. Thank you.
  • jdias
    jdias over 12 years
    Just tested and it works. Thank you Mark. I have just updated my question with your answer.
  • sehe
    sehe over 12 years
    +1 Another thinko ironed-out. Thx! (on a slightly frustrated note: this is because in git all subcommands behave slightly inconsistently - merge, stash apply, checkout with local changes, all behave differently. Not to mention the plethora of git checkout HEAD -- vs. git checkout HEAD -- ., git rebase --abort, but not git merge --abort, git push --all, but not git pull --all and many subtle instances like that :))
  • Mark Longair
    Mark Longair over 12 years
    @sehe: yes, I know what you mean - git is so elegant right up to the invocation at the command line...
  • William Leung
    William Leung almost 5 years
    git merge --abort didn't works for --squash option, so there no choice
  • William Leung
    William Leung almost 5 years
    ` git reset --merge` do not cleanup conflicts, so we have to run git clean -f -d to remove all conflict files