How to undo a git merge with conflicts

GitGit MergeGit Merge-Conflict

Git Problem Overview


I am on branch mybranch1. mybranch2 is forked from mybranch1 and changes were made in mybranch2.

Then, while on mybranch1, I have done git merge --no-commit mybranch2 It shows there were conflicts while merging.

Now I want do discard everything (the merge command) so that mybranch1 is back to what it was before. I have no idea how do I go about this.

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

Assuming you are using the latest git,

git merge --abort

Solution 4 - Git

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 - Git

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 

Solution 6 - Git

Sourcetree

If you not commit your merge, then just double click on another branch (=checkout) and when sourcetree ask you about discarding all changes then agree

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnshulView Question on Stackoverflow
Solution 1 - GitDaniel CassidyView Answer on Stackoverflow
Solution 2 - GitMartin GView Answer on Stackoverflow
Solution 3 - GitAdam DymitrukView Answer on Stackoverflow
Solution 4 - GitHanzla HabibView Answer on Stackoverflow
Solution 5 - GitManish Kumar SinghView Answer on Stackoverflow
Solution 6 - GitKamil KiełczewskiView Answer on Stackoverflow