337

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/… Commented Jan 24, 2011 at 17:44
  • 4
    @nvm: Nope. This is about real merge conflicts, not untracked files that'd be overwritten. Commented 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 ;) Commented Feb 17, 2021 at 16:13

2 Answers 2

560

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 
Sign up to request clarification or add additional context in comments.

7 Comments

In the second block of git commands there.. should there be a 'git fetch origin' after the second command?
@David: Yes, you should fetch from origin at some point. Sorry, I regarded it as implicit.
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.
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?
This worked to resolve a file naming case conflict on windows.
|
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 Comments

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?
Also, Antoine, if you want to take origin's version of everything, not just conflicted content, you can - see my answer.
@David You can get a recent version of git for debian from backports.debian.org
This is exactly what I was looking for!
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.