2795

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?

Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
Gwyn Morfey
  • 29,133
  • 6
  • 23
  • 21
  • 36
    I 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. – rjmunro Oct 07 '13 at 11:18
  • I 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)` – Chetabahana Apr 30 '16 at 17:32
  • 4
    Gwyn, 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 :) – Amicable Oct 06 '16 at 13:11

13 Answers13

2428

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
Robin Daugherty
  • 5,698
  • 1
  • 40
  • 50
Pat Notz
  • 186,044
  • 29
  • 86
  • 92
  • 53
    Instead 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 always `git fetch` and `git rebase origin`. This actually makes my merges and conflicts few and far between. – Kzqai May 13 '10 at 16:20
  • 7
    I agree. I also like to fetch first, and then examine the upstream changes (`git log ..@{upstream}` or `git diff ..@{upstream}`). After that, like you, I'll rebase my work. – Pat Notz May 14 '10 at 23:26
  • 2
    In addition to that I recommend to run "git clean -f" if you still have some untracked files you want to clean. – lzap Mar 11 '11 at 12:42
  • instead of `git reset --hard origin`, `git branch -r` (pick the branch name), `git reset --hard remote/branch` worked for me. – EoghanM May 10 '11 at 14:37
  • 183
    As noted in a more recent answer, as of version 1.6.1, it is possible to use 'git reset --merge' – Matt Ball Jan 23 '12 at 15:59
  • 5
    I used `git merge -X theirs remote_branch` instead of `git pull --strategy=theirs remote_branch` as `theirs` looks like an option of `recursive` – mlt Jul 02 '12 at 15:28
  • 5
    There is no strategy `theirs`. – srcspider Apr 04 '13 at 07:18
  • @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. – Jon L. Feb 23 '15 at 14:02
  • 37
    `git merge --abort` is far preferable. – Daniel Cassidy Feb 24 '16 at 15:48
  • `git fetch origin` followed by `git reset --hard origin/master` did the trick. BTW I 'am using git version 2.13.6 (Apple Git-96) – lnarasimhan Dec 04 '17 at 23:43
  • @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. – juanmf Feb 06 '20 at 07:50
  • I 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. – sk8forether Oct 24 '20 at 01:00
2126

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.

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
Carl
  • 39,407
  • 10
  • 74
  • 99
  • 3
    Interesting - but the manual scares me. When exactly is it appropriate to use? When would you have to specify the optional ``? #GitMoment :-o – conny Nov 15 '11 at 04:36
  • 1
    You'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 ) is just fine. – Carl Nov 15 '11 at 19:53
  • 50
    I wish this answer had more votes! At this point, it seems like the most relevant solution in many cases. – Jay Taylor Dec 07 '11 at 22:56
  • 1
    Even with uncommited changes git was able to restore the state before the merge. Nice! – T3rm1 Jun 12 '14 at 11:45
  • 2
    Is `git merge --abort` just a synonym for `git reset --merge`? The name certainly makes more sense, but does it have the same functionality? – Tikhon Jelvis Jul 22 '14 at 18:37
  • `git reset --merge` saved my bacon. Amazing! – Ben Liyanage Jan 27 '16 at 18:48
  • hey@Carl, can you please delete this answer? It's confusing a lot of people including myself. git merge --abort is the right answer and should be at the top. – Riz Sep 06 '16 at 17:43
  • 1
    `git merge --abort` doesn't work with octopus merge conflict, only `git reset --merge` does work. – ks1322 Feb 06 '17 at 15:22
  • @Riz: What evidence do you offer to back up your claim? – Carl Aug 13 '17 at 23:03
574
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 to git reset --merge when MERGE_HEAD is present.

http://www.git-scm.com/docs/git-merge

Honest Abe
  • 7,434
  • 4
  • 45
  • 59
ignis
  • 7,935
  • 2
  • 19
  • 19
107

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
David Precious
  • 6,374
  • 1
  • 21
  • 29
77

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
CB Bailey
  • 648,528
  • 94
  • 608
  • 638
  • 5
    thanks for the hint. doesn't this smack of a poor git user interface, though? – Peter Jan 12 '10 at 08:30
  • @Peter: I'm not convinced. The desired result is achievable with a few basic commands with simple options. What improvements would you suggest? – CB Bailey Mar 25 '10 at 11:58
  • 10
    I 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! – Peter Mar 27 '10 at 20:51
56

For git >= 1.6.1:

git merge --abort

For older versions of git, this will do the job:

git reset --merge

or

git reset --hard
Dielson Sales
  • 1,586
  • 1
  • 21
  • 23
Hanzla Habib
  • 1,707
  • 15
  • 18
55

Comments suggest that git reset --merge is an alias for git merge --abort. It is worth noticing that git merge --abort is only equivalent to git reset --merge given that a 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. They are not only old and new syntax for the same thing.

Personally, I find git reset --merge much more powerful for scenarios similar to the described one, and failed merges in general.

Martin G
  • 14,280
  • 9
  • 69
  • 82
  • 2
    what 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". – Ewoks Sep 28 '15 at 08:14
  • 1
    @Ewoks `git stash apply` caused a merge conflict for me but `git merge --abort` did not help while `git reset --merge` did. – nitzel Dec 06 '18 at 14:34
  • 1
    Had to use this to resolve a failed merge, I had no MERGE_HEAD for some reason so git merge --abort didn't work. – BjornW Mar 20 '21 at 15:43
38

You can either abort the merge step:

git merge --abort

else you can keep your changes (on which branch you are)

git checkout --ours file1 file2 ...

otherwise you can keep other branch changes

git checkout --theirs file1 file2 ...
DARK_C0D3R
  • 1,209
  • 9
  • 19
30

If you end up with merge conflict and doesn't have anything to commit, but still a merge error is being displayed. After applying all the below mentioned commands,

git reset --hard HEAD
git pull --strategy=theirs remote_branch
git fetch origin
git reset --hard origin

Please remove

.git\index.lock

File [cut paste to some other location in case of recovery] and then enter any of below command depending on which version you want.

git reset --hard HEAD
git reset --hard origin

Hope that helps!!!

Georgia
  • 1,183
  • 8
  • 19
Nirav Mehta
  • 6,465
  • 9
  • 39
  • 50
20

An alternative, which preserves the state of the working copy is:

git stash
git merge --abort
git stash pop

I generally advise against this, because it is effectively like merging in Subversion as it throws away the branch relationships in the following commit.

Alain O'Dea
  • 18,452
  • 1
  • 45
  • 69
  • I 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. – Alain O'Dea Jul 13 '10 at 18:58
  • 1
    Best answer to the question – Fouad Boukredine Feb 29 '20 at 01:36
  • How would this be different to a soft reset? A soft reset also resets the repository to head but doesn't touch the working copy. – Mecki Sep 04 '20 at 14:53
  • Yes, 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. – Alain O'Dea Sep 04 '20 at 14:56
18

Since Git 1.6.1.3 git checkout has been able to checkout from either side of a merge:

git checkout --theirs _widget.html.erb
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alain O'Dea
  • 18,452
  • 1
  • 45
  • 69
2

I found the following worked for me (revert a single file to pre-merge state):

git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*
Malcolm Boekhoff
  • 862
  • 10
  • 7
-10

Sourcetree

Because you not commit your merge, then just double click on another branch (which mean checkout it) and when sourcetree ask you about discarding all changes then agree :)

Update

I see many down-votes but any commet... I will left this answer which is addressed for those who use SourceTree as git client (as I - when I looking for solution for question asked by OP)

Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
  • The OP is obviously using the command line. There is zero value to your post. – Adrian Bartholomew Dec 15 '20 at 19:52
  • I 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 :) – Kamil Kiełczewski Dec 15 '20 at 22:33