How do I pull files from remote without overwriting local files?

后端 未结 3 992
鱼传尺愫
鱼传尺愫 2020-12-02 03:24

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

3条回答
  •  长情又很酷
    2020-12-02 04:06

    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.

提交回复
热议问题