I used git pull
and had a merge conflict:
unmerged: some_file.txt
You are in the middle of a conflicted merge.
How do I abandon my changes to the file and keep only the pulled changes?
I used git pull
and had a merge conflict:
unmerged: some_file.txt
You are in the middle of a conflicted merge.
How do I abandon my changes to the file and keep only the pulled changes?
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
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. –
Quinlan git log ..@{upstream}
or git diff ..@{upstream}
). After that, like you, I'll rebase my work. –
Wieche git reset --hard origin
, git branch -r
(pick the branch name), git reset --hard remote/branch
worked for me. –
Wollis git merge -X theirs remote_branch
instead of git pull --strategy=theirs remote_branch
as theirs
looks like an option of recursive
–
Maronite theirs
. –
Labiodental theirs
is indeed an option of recursive, it doesn't exist as a strategy of it's own (in current git), per the docs. –
Advent git merge --abort
is far preferable. –
Significative 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) –
Obelisk git pull --rebase
once I have locally committed (normally amending so I have 1 commit per feature, at least until I push) my changes. –
Dressel git merge --abort
did not work, these steps did. –
Pontone 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.
<commit>
? #GitMoment :-o –
Technicolor git merge --abort
just a synonym for git reset --merge
? The name certainly makes more sense, but does it have the same functionality? –
Examinee git merge --abort
doesn't work with octopus merge conflict, only git reset --merge
does work. –
Cadelle 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.
git stash pop
. –
Neoimpressionism 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.
git revert
does not undo the commit, it undo the changes that that commit introduced. And it does that by creating a new commit with the opposite changes than the target commit. –
Quenchless 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
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 ...
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
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! –
Alonaalone :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! –
Stan 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.
git stash apply
caused a merge conflict for me but git merge --abort
did not help while git reset --merge
did. –
Immunity stash pop
; and reset --merge
will delete your untracked files. See https://mcmap.net/q/11332/-undo-git-stash-pop-that-results-in-merge-conflict –
Alongshore 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!!!
Could not find merge strategy 'theirs'.
–
Selfexcited 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.
Might not be what the OP wanted, but for me I tried to merge a stable branch to a feature branch and there were too many conflicts. I didn't manage to reset the changes since the HEAD was changed by many commits, So the easy solution was to force checkout to a stable branch. you can then checkout to the other branch and it will be as it was before the merge.
git checkout -f master
git checkout side-branch
git checkout -f main
for the post-2020 copy-pasters. –
Dispersoid 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
To avoid getting into this sort of trouble one can expand on the git merge --abort
approach and create a separate test branch before merging.
Case: You have a topic branch, it wasn't merged because you got distracted/something came up/you know but it is (or was) ready.
Now is it possible to merge this into master?
Work in a test branch to estimate / find a solution, then abandon the test branch and apply the solution in the topic branch.
# Checkout the topic branch
git checkout topic-branch-1
# Create a _test_ branch on top of this
git checkout -b test
# Attempt to merge master
git merge master
# If it fails you can abandon the merge
git merge --abort
git checkout -
git branch -D test # we don't care about this branch really...
Work on resolving the conflict.
# Checkout the topic branch
git checkout topic-branch-1
# Create a _test_ branch on top of this
git checkout -b test
# Attempt to merge master
git merge master
# resolve conflicts, run it through tests, etc
# then
git commit <conflict-resolving>
# You *could* now even create a separate test branch on top of master
# and see if you are able to merge
git checkout master
git checkout -b master-test
git merge test
Finally checkout the topic branch again, apply the fix from the test branch and continue with the PR. Lastly delete the test and master-test.
Involved? Yes, but it won't mess with my topic or master branch until I'm good and ready.
I found the following worked for me (revert a single file to pre-merge state):
git reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*
© 2022 - 2024 — McMap. All rights reserved.
[rejected] gh-pages -> gh-pages (non-fast-forward)
– Aestivalgit merge --abort
? – Greenwood