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.

  • Is there a git command that attempts a pull before pushing everytime?
  • What way to choose in case of merging in my case?
  • Explanation of 1 commit ahead of master and 1 commit behind master
  • Should you git branch if the intention is to change the master branch drastically
  • .gitattributes merge driver is not used
  • Creating an orphan branch but still be able to merge from another origin
  • Git: Split subdirectory into repo and continue merging
  • tortoiseGit Reset to a previous version
  • 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.