182

How can I tell git to ignore my local file and take the one from my remote branch without trying to merge and causing conflicts?

ryudice
  • 36,476
  • 32
  • 115
  • 163
  • 1
    See SO answer "**[git command for making one branch like another](http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another/4912267#4912267)**" for **all** the current possible ways to **simulate `git merge -s their`**. – VonC Mar 13 '11 at 11:04
  • It would be helpful if you could provide some more details. Do you want to adopt the content of all of the files from remote branch, or just some of the files (i.e. keep some local versions/changes)? Do you have any local history that you want to keep? (This might be important if there are any other branches or repositories that have already incorporated your local history.) If you want to keep your local history, what do you plan on doing with it later? (You could leave a tag pointing to the local history and just reset your branch to what the remote has, or you might want to “merge theirs”). – Chris Johnsen Mar 14 '11 at 05:12
  • 1
    There is also a thread like this at question [Reset local repository branch to be just like remote repository HEAD](http://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head) – mico Mar 14 '17 at 16:00

6 Answers6

209

This is the safest solution:

git stash

Now you can do whatever you want without fear of conflicts.

For instance:

git checkout origin/master   # or origin/main

If you want to include the remote changes in the master branch you can do:

git reset --hard origin/master  # or origin/main

This will make you branch "master" to point to "origin/master".

floer32
  • 2,190
  • 4
  • 29
  • 50
Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
52

I understand the question as this: you want to completely replace the contents of one file (or a selection) from upstream. You don't want to affect the index directly (so you would go through add + commit as usual).

Simply do

git checkout remote/branch -- a/file b/another/file

If you want to do this for extensive subtrees and instead wish to affect the index directly use

git read-tree remote/branch:subdir/

You can then (optionally) update your working copy by doing

git checkout-index -u --force
sehe
  • 374,641
  • 47
  • 450
  • 633
19

My understanding is that, for example, you wrongly saved a file you had updated for testing purposes only. Then, when you run "git status" the file appears as "Modified" and you say some bad words. You just want the old version back and continue to work normally.

In that scenario you can just run the following command:

git checkout -- path/filename
Almir Campos
  • 2,833
  • 1
  • 30
  • 26
11

I would checkout the remote file from the "master" (the remote/origin repository) like this:

git checkout master <FileWithPath>

Example: git checkout master components/indexTest.html

Birol Efe
  • 1,605
  • 16
  • 14
5

Use the -s or --strategy option combined with the -X option. In your specific question, you want to keep all of the remote files and replace the local files of the same name.

Replace conflicts with the remote version

git merge -s recursive -Xtheirs upstream/master  

will use the remote repo version of all conflicting files.

Replace conflicts with the local version

git merge -s recursive -Xours upstream/master

will use the local repo version of all conflicting files.

csi
  • 9,018
  • 8
  • 61
  • 81
-1

Rename the old local branch

git branch -m master old_master

Checkout branch from remote

git checkout -b master origin/master
ZuLu
  • 933
  • 8
  • 17