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.

  • How to generate diff patch file for only committed files?
  • Display latest tag in a commit with multiple tags
  • What is the meaning of the “bang” or “!” before the git command?
  • Only use a proxy for certain git urls/domains?
  • Run autotests as a result of clicking 'merge pull request' button
  • How to ensure everything has been pushed with git?
  • Is there any way to pull but make sure that the local files are not overwritten by the remote?

  • GitLab cloning not successful
  • OpenWRT + https + git
  • Git for Windows: .bashrc or equivalent config files for Git Bash shell
  • use hub, the github-gem or just stick to git when working with github repo's?
  • Git add Folder with Existing Git Repository
  • Git checkout delete files in gitignore
  • 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…”

    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.

    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.

    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? 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.

    Git Baby is a git and github fan, let's start git clone.