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?
Since your
pull
was unsuccessful thenHEAD
(notHEAD^
) is the last "valid" commit on your branch: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:
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:
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 togit reset --merge
whenMERGE_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 andgit stash pop
after finishing the merge or aborting it.Since comments suggest that
git reset --merge
is an alias forgit merge --abort
, it is worth noticing thatgit merge --abort
is only equivalent togit reset --merge
given that aMERGE_HEAD
is present. This can be read in the git help for merge command.After a failed merge, when there is no
MERGE_HEAD
, the failed merge can be undone withgit reset --merge
but not necessarily withgit merge --abort
, so 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.I found the following worked for me (revert a single file to pre-merge state):
Its so simple.
Git itself shows you the solution when you are in this type of trouble and run the git status command.
Hope this will help people.
Since Git 1.6.1.3
git checkout
has been able to checkout from either side of a merge: