Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I ran into a merge conflict. How can I abort the merge?

I used git pull and had a merge conflict:

unmerged:   _widget.html.erb  You are in the middle of a conflicted merge. 

I know that the other version of the file is good and that mine is bad so all my changes should be abandoned. How can I do this?

like image 295
Gwyn Morfey Avatar asked Sep 19 '08 13:09

Gwyn Morfey


People also ask

How do you abort a merge conflict?

You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.

Can I cancel merge in progress?

If there are any merges in progress, you will see the merge in progress notification in the status field. Right click on merge in progress notification and cancel it.

How do I undo a merge attempt?

Git merge --abort # this will allow you to undo merge conflicts. 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, Generally, you shouldn't merge with uncommitted changes anyway.

How do you escape a merge?

write your merge message. press "esc" (escape) write ":wq" (write & quit) then press enter.


2 Answers

Since your pull was unsuccessful then HEAD (not HEAD^) is the last "valid" commit on your branch:

git reset --hard HEAD 

The other piece you want is to let their changes over-ride your changes.

Older versions of git allowed you to use the "theirs" merge strategy:

git pull --strategy=theirs remote_branch 

But this has since been removed, as explained in this message by Junio Hamano (the Git maintainer). As noted in the link, instead you would do this:

git fetch origin git reset --hard origin 
like image 97
Pat Notz Avatar answered Oct 07 '22 23:10

Pat Notz


If your git version is >= 1.6.1, you can use git reset --merge.

Also, as @Michael Johnson mentions, if your git version is >= 1.7.4, you can also use git merge --abort.

As always, make sure you have no uncommitted changes before you start a merge.

From the git merge man page

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

MERGE_HEAD is present when a merge is in progress.

Also, regarding uncommitted changes when starting a merge:

If you have changes you don't want to commit before starting a merge, just git stash them before the merge and git stash pop after finishing the merge or aborting it.

like image 45
Carl Avatar answered Oct 08 '22 00:10

Carl