165

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?

4 Answers 4

257

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.

Sign up to request clarification or add additional context in comments.

9 Comments

why not just git commit local changes beforing git pull ?
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.
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...
"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.
This is the most excellent and concise way to explain the flow of the question - and I had the same question too. Thanks !
|
33

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.

4 Comments

I have already committed these changes locally so that it says there are "no local changes to save"
i think , this is the best strategy.
Can we also add git pull --rebase --autostash option?
This does not work if the local file was replaced with a symbolic link.
19

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.

2 Comments

thanks for explaining git pull as a two-step git fetch/git merge
On git version 2.25.1 having local files which conflict is not resolved by fetch and then git merge origin/[branch]
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 -- . 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.