How to undo a git merge with conflicts
Solution 1
Latest Git:
git merge --abort
This attempts to reset your working copy to whatever state it was in before the merge. That means that it should restore any uncommitted changes from before the merge, although it cannot always do so reliably. Generally you shouldn't merge with uncommitted changes anyway.
Prior to version 1.7.4:
git reset --merge
This is older syntax but does the same as the above.
Prior to version 1.6.2:
git reset --hard
which removes all uncommitted changes, including the uncommitted merge. Sometimes this behaviour is useful even in newer versions of Git that support the above commands.
Solution 2
Actually, it is worth noticing that git merge --abort
is only equivalent to git reset --merge
given that MERGE_HEAD
is present. This can be read in the git help for merge command.
git merge --abort # is equivalent to git reset --merge when MERGE_HEAD is present.
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.
Personally I find git reset --merge
much more useful in everyday work.
Solution 3
Assuming you are using the latest git,
git merge --abort
Solution 4
If using latest Git,
git merge --abort
else this will do the job in older git versions
git reset --merge
or
git reset --hard
Solution 5
There are two things you can do first undo merge by command
git merge --abort
or
you can go to your previous commit state temporarily by command
git checkout 0d1d7fc32
Admin
Updated on June 26, 2020Comments
-
Admin almost 4 years
I am on branch
mybranch1
.mybranch2
is forked frommybranch1
and changes were made inmybranch2
.Then, while on
mybranch1
, I have donegit merge --no-commit mybranch2
It shows there were conflicts while merging.Now I want do discard everything (the
merge
command) so thatmybranch1
is back to what it was before. I have no idea how do I go about this. -
Admin about 13 yearsOk this doesn't work with 1.7.0.7 :( . got to use reset --hard with it
-
Admin about 13 yearsBut for older versions of git this is the way to use
-
geomaster over 8 yearsThank you, this info was very helpful. I had a merge that started with
git stash apply
on a wrong branch, andgit merge --abort
did nothing (noMERGE_HEAD
), whilegit reset --merge
did the trick. -
Theodore Murdock over 8 yearsSometimes you still need to use
git reset --merge
even in more recent versions. I've hadgit merge --abort
error out (making no changes) wheregit reset --merge
succeeds (and does the right thing) in git 2.2.1. -
Tom over 7 yearsI've seen about 10 people saying
git merge --abort
is the new command forgit reset --merge
and I ran into the same issue as @geomaster this was super helpful thanks! -
Chef Pharaoh about 6 yearsI found I needed to do
git merge --abort
followed bygit reset --merge
when having automerge conflict from popping my stash. -
yano over 5 yearsusually
git merge --abort
works for me, however I found myself in a situation where I checked out in a detached HEAD state, and one of my files had a "both modified" state. I wanted to discard everything and get back to a branch, I had togit reset --hard
,git merge --abort
told me there was no merge to abort, (MERGE_HEAD missing). -
Kevin Stewart about 5 yearsSometimes
git merge --abort
isn't able to bring you back your previous state, and in that case the "older syntax"git reset --hard
does the trick. -
vikramvi about 3 yearsGot message "fatal: There is no merge to abort (MERGE_HEAD missing)."
-
A-S over 2 yearsFor me, with
git
version2.31.1
, that was released later than this comment so I consider it "latest", onlygit reset --hard
worked, and the rest didn't.