159

I am trying to set up a new git repo to a pre-existing remote repo.

I want my local files to overwrite the remote repo, but git says that I first have to pull those remote files in and merge them.

Is there any way to pull but make sure that the local files are not overwritten by the remote?

7ochem
  • 2,183
  • 1
  • 34
  • 42
Joe Isaacson
  • 4,012
  • 6
  • 30
  • 40

4 Answers4

248

Well, yes, and no...

I understand that you want your local copies to "override" what's in the remote, but, oh, man, if someone has modified the files in the remote repo in some different way, and you just ignore their changes and try to "force" your own changes without even looking at possible conflicts, well, I weep for you (and your coworkers) ;-)

That said, though, it's really easy to do the "right thing..."

Step 1:

git stash

in your local repo. That will save away your local updates into the stash, then revert your modified files back to their pre-edit state.

Step 2:

git pull

to get any modified versions. Now, hopefully, that won't get any new versions of the files you're worried about. If it doesn't, then the next step will work smoothly. If it does, then you've got some work to do, and you'll be glad you did.

Step 3:

git stash pop

That will merge your modified versions that you stashed away in Step 1 with the versions you just pulled in Step 2. If everything goes smoothly, then you'll be all set!

If, on the other hand, there were real conflicts between what you pulled in Step 2 and your modifications (due to someone else editing in the interim), you'll find out and be told to resolve them. Do it.

Things will work out much better this way - it will probably keep your changes without any real work on your part, while alerting you to serious, serious issues.

Amol Ghotankar
  • 2,008
  • 5
  • 28
  • 42
Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
  • 6
    why not just `git commit` local changes beforing `git pull` ? – Don Cheadle Oct 20 '14 at 14:58
  • 15
    Sometimes I am in a position where I don't want to commit the code, but I would like to keep it in my local machine, these set of commands is really helpful for this. – Vargan Feb 27 '15 at 11:30
  • 4
    This is not useful if you have multiple files, some that you'd like to have overwritten and some that you don't want to overwrite. Is there no way to tell GIT ignore files on pull? Why does .gitignore only work for pushing, I don't understand that design decision at all... – CasualScience Feb 10 '16 at 19:20
  • 2
    "you just ignore their changes" is an unfair observation, I face this problem with Visual Studio which generates a specific "csproj" tailored for the local workstation depending in library references. And I don't want to push those changes to the server either. – snesgx Aug 21 '20 at 17:21
  • This is the most excellent and concise way to explain the flow of the question - and I had the same question too. Thanks ! – AlexD Dec 17 '20 at 17:23
  • consider using `git stash drop` after the step3 if you would like to remove the last stash you made from the `refs/stash` branch – NAND Jan 31 '23 at 12:55
  • I tried this but it added merge conflict stuff to all my files instead of overwriting them with the stashed ones... – stackers Jul 18 '23 at 13:44
32

You can stash your local changes first, then pull, then pop the stash.

git stash
git pull origin master
git stash pop

Anything that overrides changes from remote will have conflicts which you will have to manually resolve.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
18

So you have committed your local changes to your local repository. Then in order to get remote changes to your local repository without making changes to your local files, you can use git fetch. Actually git pull is a two step operation: a non-destructive git fetch followed by a git merge. See What is the difference between 'git pull' and 'git fetch'? for more discussion.

Detailed example:

Suppose your repository is like this (you've made changes test2:

* ed0bcb2 - (HEAD, master) test2
* 4942854 - (origin/master, origin/HEAD) first

And the origin repository is like this (someone else has committed test1):

* 5437ca5 - (HEAD, master) test1
* 4942854 - first

At this point of time, git will complain and ask you to pull first if you try to push your test2 to remote repository. If you want to see what test1 is without modifying your local repository, run this:

$ git fetch

Your result local repository would be like this:

* ed0bcb2 - (HEAD, master) test2 
| * 5437ca5 - (origin/master, origin/HEAD) test1 
|/  
* 4942854 - first 

Now you have the remote changes in another branch, and you keep your local files intact.

Then what's next? You can do a git merge, which will be the same effect as git pull (when combined with the previous git fetch), or, as I would prefer, do a git rebase origin/master to apply your change on top of origin/master, which gives you a cleaner history.

Penghe Geng
  • 13,286
  • 5
  • 31
  • 40
8

All of the other answers still can have merge conflicts. If you really don't want to deal with conflicts, and simply pull but not overwrite local files with changes the way to do it is like this:

Stash your local changes:

git stash

Pull everything (If it gives you conflicts just do git reset --hard HEAD, you saved your changes with the stash)

git pull --force

Now force apply your stash (no conflicts!)

git checkout stash -- .
Explosion
  • 328
  • 2
  • 10