I ran into a merge conflict. How can I abort the merge?
Solution 1
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
Solution 2
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.
Solution 3
git merge --abort
Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
If there were uncommitted worktree changes present when the merge started,
git merge --abort
will in some cases be unable to reconstruct these changes. It is therefore recommended to always commit or stash your changes before running git merge.
git merge --abort
is equivalent togit reset --merge
whenMERGE_HEAD
is present.
http://www.git-scm.com/docs/git-merge
Solution 4
I think it's git reset
you need.
Beware that git revert
means something very different to, say, svn revert
- in Subversion the revert will discard your (uncommitted) changes, returning the file to the current version from the repository, whereas git revert
"undoes" a commit.
git reset
should do the equivalent of svn revert
, that is, discard your unwanted changes.
Solution 5
In this particular use case, you don't really want to abort the merge, just resolve the conflict in a particular way.
There is no particular need to reset and perform a merge with a different strategy, either. The conflicts have been correctly highlighted by git and the requirement to accept the other sides changes is only for this one file.
For an unmerged file in a conflict git makes available the common base, local and remote versions of the file in the index. (This is where they are read from for use in a 3-way diff tool by git mergetool
.) You can use git show
to view them.
# common base:
git show :1:_widget.html.erb
# 'ours'
git show :2:_widget.html.erb
# 'theirs'
git show :3:_widget.html.erb
The simplest way to resolve the conflict to use the remote version verbatim is:
git show :3:_widget.html.erb >_widget.html.erb
git add _widget.html.erb
Or, with git >= 1.6.1:
git checkout --theirs _widget.html.erb
NicW
Updated on July 08, 2022Comments
-
NicW almost 2 years
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?
-
rjmunro over 10 yearsI realise this is a super-old question, but do you want to abort the whole merge, and leave the branch you were merging unmerged, or just ignore this one file as part of a larger merge, letting all the other files merge in as normal? To me, your title implies the former, your question body wants the latter. The answers do both, without making things clear.
-
eQ19 about 8 yearsI got similar case on commit saying that automatic merge failed; fix conflicts and then commit the result:
[rejected] gh-pages -> gh-pages (non-fast-forward)
-
Amicable over 7 yearsGwyn, it could be useful to select an accepted answer here. The top voted one is a bit less safe than some of the more up to date solutions, so I think it would help to highlight others over it :)
-
-
Peter over 14 yearsthanks for the hint. doesn't this smack of a poor git user interface, though?
-
CB Bailey about 14 years@Peter: I'm not convinced. The desired result is achievable with a few basic commands with simple options. What improvements would you suggest?
-
Peter about 14 yearsI think the
git 1.6.1
command makes a lot of sense, and is good. That's exactly what I would have wanted. I think the pre-1.6.1 solution is inelegant and requires knowledge about other parts of git that should be separated from the merge resolution process. But the new version is great! -
Kzqai almost 14 yearsInstead of doing a hard reset, you could bring it to a more granular level by doing:
git fetch origin
-->git reset origin (soft reset, your changes are still present)
-->git checkout file_to_use_their_version_of another_file (steamroll your own changes back to match the origin)
I never use git pull any more. Since in a fight between my latest code and the origin, the origin should always win, I alwaysgit fetch
andgit rebase origin
. This actually makes my merges and conflicts few and far between. -
Pat Notz almost 14 yearsI agree. I also like to fetch first, and then examine the upstream changes (
git log ..@{upstream}
orgit diff ..@{upstream}
). After that, like you, I'll rebase my work. -
ThanksForYourHelp almost 14 yearsI found this approach useful when I accidentally merged to a git-svn branch, which doesn't handle that nicely. Squash merges or cherry picks are better when working with git-svn tracking branches. In effect my solution turns a merge into a squash merge after the fact.
-
lzap about 13 yearsIn addition to that I recommend to run "git clean -f" if you still have some untracked files you want to clean.
-
EoghanM almost 13 yearsinstead of
git reset --hard origin
,git branch -r
(pick the branch name),git reset --hard remote/branch
worked for me. -
conny over 12 yearsInteresting - but the manual scares me. When exactly is it appropriate to use? When would you have to specify the optional
<commit>
? #GitMoment :-o -
Carl over 12 yearsYou'd typically use this when you want to redo the merge from the start. I have never had to specify the optional commit myself, so the default (no optional <commit>) is just fine.
-
Jay Taylor over 12 yearsI wish this answer had more votes! At this point, it seems like the most relevant solution in many cases.
-
Matt Ball over 12 yearsAs noted in a more recent answer, as of version 1.6.1, it is possible to use 'git reset --merge'
-
mlt almost 12 yearsI used
git merge -X theirs remote_branch
instead ofgit pull --strategy=theirs remote_branch
astheirs
looks like an option ofrecursive
-
srcspider about 11 yearsThere is no strategy
theirs
. -
Michael Johnson about 11 yearsThis has been available since git v1.7.4. It's an alias for git reset --merge.
-
T3rm1 almost 10 yearsEven with uncommited changes git was able to restore the state before the merge. Nice!
-
Tikhon Jelvis almost 10 yearsIs
git merge --abort
just a synonym forgit reset --merge
? The name certainly makes more sense, but does it have the same functionality? -
Jon L. about 9 years@mlt,
theirs
is indeed an option of recursive, it doesn't exist as a strategy of it's own (in current git), per the docs. -
Ewoks over 8 yearswhat is here actually meant by "failed merge"? Merge with conflicts or something else? Or to rephrase it: when is MERGE_HEAD not present? My follow-up question is there to understand better use of "git reset --merge".
-
Ben Liyanage over 8 years
git reset --merge
saved my bacon. Amazing! -
Daniel Cassidy about 8 years
git merge --abort
is far preferable. -
ks1322 about 7 years
git merge --abort
doesn't work with octopus merge conflict, onlygit reset --merge
does work. -
lnarasimhan over 6 years
git fetch origin
followed bygit reset --hard origin/master
did the trick. BTW I 'am using git version 2.13.6 (Apple Git-96) -
nitzel over 5 years@Ewoks
git stash apply
caused a merge conflict for me butgit merge --abort
did not help whilegit reset --merge
did. -
juanmf about 4 years@Kzqai I use
git pull --rebase
once I have locally committed (normally amending so I have 1 commit per feature, at least until I push) my changes. -
Fouad Boukredine about 4 yearsBest answer to the question
-
Mecki over 3 yearsHow would this be different to a soft reset? A soft reset also resets the repository to head but doesn't touch the working copy.
-
ThanksForYourHelp over 3 yearsYes, but does "git reset --soft someref" spring to mind when your goal is "How can I abort the merge?". How do you know what to use for someref? Hence "git merge --abort" which does the right thing and is obviously named which is refreshing for Git.
-
sk8forether over 3 yearsI would like to note for the next poor soul who ends up in my situation that when
git merge --abort
did not work, these steps did. -
Adrian Bartholomew over 3 yearsThe OP is obviously using the command line. There is zero value to your post.
-
Kamil Kiełczewski over 3 yearsI agree that this is obvious that OP use command line - but people who not use command line but sourcetree and have similar problem find this question at the top in google (like I) - so for such persons this answer has value - this is why I left it here :)
-
BjornW about 3 yearsHad to use this to resolve a failed merge, I had no MERGE_HEAD for some reason so git merge --abort didn't work.
-
Luis over 2 yearsHeads up to anyone else considering this: MERGE_HEAD is not present when your merge happened because of
stash pop
; andreset --merge
will delete your untracked files. See stackoverflow.com/a/67099267/1623757 -
Kheldar over 2 yearsWhat I needed after a failed merge from a tag to master...
-
Carmine Tambascia about 2 yearsNo really because a merge conflict could be for different reasons. Nevertheless behind what you do on GUI in SourceTree, are running command line command. On top I would say be careful with GUI because any unexpected such low internet connection will cause your GUI to go out of sync with git index and you may face a corrupted flow with a git.lock that is hard to "fix" except to remove it, and abort the merge conflict
-
Steve Summit about 2 yearsWell, without commenting on whether it's "poor" or not, let me say that I would never have guessed that a syntax involving 'show' and
:1:
,:2:
, and:3:
was the way to recover the base and two "tip" files, but I am immensely glad to know of this technique, so: thank you very much!