lizarisk
lizarisk

Reputation: 7820

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.

Upvotes: 2

Views: 696

Answers (3)

Sajib Khan
Sajib Khan

Reputation: 24156

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.

Upvotes: 2

Josh Lee
Josh Lee

Reputation: 177574

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").

Upvotes: 2

Vampire
Vampire

Reputation: 38639

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.

Upvotes: 1

Related Questions