973

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 to 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.

Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
Anshul
  • 9,731
  • 3
  • 15
  • 4
  • 1
    Possible duplicate of [I ran into a merge conflict. How can I abort the merge?](http://stackoverflow.com/questions/101752/i-ran-into-a-merge-conflict-how-can-i-abort-the-merge) – Suma Jan 20 '16 at 21:15

7 Answers7

1664

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.

Daniel Cassidy
  • 24,676
  • 5
  • 41
  • 54
  • 1
    But for older versions of git this is the way to use – Anshul Apr 21 '11 at 08:59
  • 9
    Sometimes you still need to use `git reset --merge` even in more recent versions. I've had `git merge --abort` error out (making no changes) where `git reset --merge` succeeds (and does the right thing) in git 2.2.1. – Theodore Murdock Nov 18 '15 at 19:26
  • 1
    I found I needed to do `git merge --abort` followed by `git reset --merge` when having automerge conflict from popping my stash. – Chef Pharaoh Apr 19 '18 at 15:04
  • 1
    usually `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 to `git reset --hard`, `git merge --abort` told me there was no merge to abort, (MERGE_HEAD missing). – yano Dec 17 '18 at 18:20
  • Sometimes `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. – Kevin Stewart Feb 21 '19 at 15:56
  • Got message "fatal: There is no merge to abort (MERGE_HEAD missing)." – vikramvi Mar 03 '21 at 11:05
160

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.

CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 2
    Thank you, this info was very helpful. I had a merge that started with `git stash apply` on a wrong branch, and `git merge --abort` did nothing (no `MERGE_HEAD`), while `git reset --merge` did the trick. – geomaster Sep 10 '15 at 02:02
  • 5
    I've seen about 10 people saying `git merge --abort` is the new command for `git reset --merge` and I ran into the same issue as @geomaster this was super helpful thanks! – Tom Jan 20 '17 at 20:26
  • Tried this after a `git checkout --merge` which resulted in conflicts, ended up losing data. git docs say it will keep worktree files which differ from the index, which it does, but for any conflicted file, you get the checkout version, and the worktree version is completely lost. – davenpcj May 09 '23 at 23:11
114

Assuming you are using the latest git,

git merge --abort
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
67

If using latest Git,

git merge --abort

else this will do the job in older git versions

git reset --merge

or

git reset --hard
Hanzla Habib
  • 3,457
  • 25
  • 25
  • 2
    For me, with `git` version `2.31.1`, that was released later than this comment so I consider it "latest", only `git reset --hard` worked, and the rest didn't. – A-S Oct 01 '21 at 23:54
9

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 
0

Since nobody mentioned this before, if you get this error

fatal: Could not reset index file to revision 'HEAD'.

when trying the above methods you can try adding the files (staging them) and then instead of committing just do git stash you can use the -m flag and give it a tag so you know if you do by chance need to recover that last state you can find the stash with git stash list.

For me, git reset --merge and git merge --abort failed with above error.

Mihai
  • 82
  • 1
  • 6
-1

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

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345