How to reset to the state before a merge was committed?

I’ve resolved some conflicts and committed a merge. Is it possible to restore exactly the merge state that was before running git commit, not changing the index or the working tree? For regular single-parent commits, the command which does this is git reset --soft HEAD^, but for merge commits it doesn’t work as expected, because there is no single parent to reset to.

  • Git 'fatal: No such ref: HEAD'
  • How to modify a specified commit in git?
  • GitFlow: safely merge develop changes to a feature branch
  • DCVS + hosting for a startup commercial multiplatform phone app
  • failed to push some refs to
  • How to get a copy of an older version of a file in a git repository?
  • How can I set up Git for local commits while using P4 for remote commits?
  • The difference between a/ and a/* and a/** in .gitignore?
  • With git, how do I reset the local deployment branch to origin's version?
  • Unable to run nodejs project - npm ERR! code EBADPLATFORM
  • git cherry-pick -x: link in details instead of in summary
  • String comparison not working in PowerShell function - what am I doing wrong?
  • 3 Solutions collect form web for “How to reset to the state before a merge was committed?”

    I don’t think this is supported, but you can fake it by doing git reset --soft HEAD@{1} and then writing the commit id that you were merging to .git/MERGE_HEAD.

    You’ll lose the commit message, though (“Merge commit ‘cafebab’ into HEAD”).

    You have a commit-sha for your merge state. Find the commit by git log or, git reflog command.

    $ git reflog                       # copy the commit-sha of merge state
    $ git reset --soft <commit-sha>
    $ git status                       # see the undo changes

    N.B. If you do --soft reset then the changes of the commits you back exists in your working directory.

    git reset --soft HEAD^ is a synonym for git reset --soft HEAD^1 which means reset softly to the first parent. If it is the second parent (second parent != grandparent, that would be HEAD~2) you want to reset to, use git reset --soft HEAD^2. Alternatively you can of course also do git reset --soft @{1} which means the first entry in the reflog of the current branch if the merge was the last thing you did.

    Git Baby is a git and github fan, let's start git clone.