Resolve conflicts using remote changes when pulling from Git remote
Asked Answered
A

2

334

I'm trying to pull code from my GitHub repo onto my server, but the pull keeps failing because of merge conflicts. I don't want to keep any of the changes that may have occurred on my local server since the last pull.

So is there a way I can force Git to overwrite with whatever version is in GitHub, rather than bother me about conflicts?

Aromaticity answered 24/1, 2011 at 17:36 Comment(3)
duplicate? stackoverflow.com/questions/4779715/…Rosefish
@nvm: Nope. This is about real merge conflicts, not untracked files that'd be overwritten.Ula
@Rosefish if this is a duplicate, then other way round. I think its unrelated. But good you answered the other question ;)Buyse
U
558

If you truly want to discard the commits you've made locally, i.e. never have them in the history again, you're not asking how to pull - pull means merge, and you don't need to merge. All you need do is this:

# fetch from the default remote, origin
git fetch
# reset your current branch (master) to origin's master
git reset --hard origin/master

I'd personally recommend creating a backup branch at your current HEAD first, so that if you realize this was a bad idea, you haven't lost track of it.

If on the other hand, you want to keep those commits and make it look as though you merged with origin, and cause the merge to keep the versions from origin only, you can use the ours merge strategy:

# fetch from the default remote, origin
git fetch
# create a branch at your current master
git branch old-master
# reset to origin's master
git reset --hard origin/master
# merge your old master, keeping "our" (origin/master's) content
git merge -s ours old-master
Ula answered 24/1, 2011 at 21:26 Comment(7)
In the second block of git commands there.. should there be a 'git fetch origin' after the second command?Aromaticity
@David: Yes, you should fetch from origin at some point. Sorry, I regarded it as implicit.Ula
There's nothing that can be left implied when it comes to me and git ;-). Seriously though, thanks a million. Your answer's are exactly what I was looking for.Aromaticity
Will this work if origin is actually ahead? as in, can I also use it if I don't have any commits ahead, and in fact the branch can be fast-forwarded?Irons
This worked to resolve a file naming case conflict on windows.Verwoerd
Why create a back-up? if you screw-up can't you just rely on the reflog to "go back in time" so to speak?Arnaldo
Now it's called origin/main, not origin/master anymore.Jerroldjerroll
C
162

You can either use the answer from the duplicate link pointed by nvm.

Or you can resolve conflicts by using their changes (but some of your changes might be kept if they don't conflict with remote version):

git pull -s recursive -X theirs
Calvillo answered 24/1, 2011 at 18:3 Comment(11)
Doesn't seem to be working for me. I get "error: unknown switch `X'" using git git version 1.5.6.5. Do I need to upgrade to an unstable version?Aromaticity
Also, Antoine, if you want to take origin's version of everything, not just conflicted content, you can - see my answer.Ula
And finally, nvm's answer is not a duplicate. That wasn't an instance of merge conflicts (content tracked on both sides, with differences) but rather a merge that would overwrite untracked content.Ula
Yes, I like your answer. Very interesting use of the ours strategy.Calvillo
The use of the ours strategy is more obvious if you remember that the right direction to merge is almost universally from topic branch into master.Ula
Sorry when I said 'unstable' all I meant was that there doesn't seem to be a more recent stable release of git in the Debian packages repository: packages.debian.org/lenny/git-coreAromaticity
@David You can get a recent version of git for debian from backports.debian.orgDogfight
-s recursive is the default, so git pull -X theirs is enough.Arnaud
This is exactly what I was looking for!Satyriasis
@CeesTimmerman Not true, at least in latest git. X option is passed through to merge strategy, which is only recursive if merging two heads, so your command will complain "Could not find merge strategy 'theirs'. Available strategies are: octopus ours recursive resolve subtree." - it's a shame, because X can be set in config (e.g. git config pull.twohead theirs) but s cannot.Lionellionello
Unfortunately this doesn't work for me. I still have: error: Your local changes to the following files would be overwritten by merge: file.txt Please commit your changes or stash them before you merge.Delaware

© 2022 - 2024 — McMap. All rights reserved.