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?

3
  • duplicate? stackoverflow.com/questions/4779715/…
    – user173973
    Jan 24, 2011 at 17:44
  • 4
    @nvm: Nope. This is about real merge conflicts, not untracked files that'd be overwritten.
    – Cascabel
    Jan 24, 2011 at 21:32
  • @user173973 if this is a duplicate, then other way round. I think its unrelated. But good you answered the other question ;) Feb 17, 2021 at 16:13

2 Answers 2

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
7
  • 1
    In the second block of git commands there.. should there be a 'git fetch origin' after the second command? Jan 27, 2011 at 10:50
  • @David: Yes, you should fetch from origin at some point. Sorry, I regarded it as implicit.
    – Cascabel
    Jan 27, 2011 at 15:17
  • 2
    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. Jan 27, 2011 at 15:23
  • 1
    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? Oct 12, 2013 at 23:37
  • This worked to resolve a file naming case conflict on windows.
    – Will B.
    Dec 19, 2016 at 14:07
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
11
  • 3
    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? Jan 24, 2011 at 18:52
  • Also, Antoine, if you want to take origin's version of everything, not just conflicted content, you can - see my answer.
    – Cascabel
    Jan 24, 2011 at 21:31
  • 2
    @David You can get a recent version of git for debian from backports.debian.org Jan 27, 2011 at 21:12
  • 2
    This is exactly what I was looking for!
    – micahblu
    Jul 15, 2016 at 23:28
  • 2
    @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.
    – OJFord
    Jan 13, 2017 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.