How do I pull files from remote without overwriting local files?
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?
3 Solutions collect form web for “How do I pull files from remote without overwriting local files?”
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…”
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.
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.
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.
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.
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.
Suppose your repository is like this (you’ve made changes
* ed0bcb2 - (HEAD, master) test2 * 4942854 - (origin/master, origin/HEAD) first
origin repository is like this (someone else has committed
* 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? Usually you would want to merge your changes with remote changes and then commit and push. But if you don’t want to use your changes exclusively (if that’s what your overwrite the remote repo means), you will have to create a new branch (other than
master) for your
test2, and push that branch to the remote repository.