Different default remote (tracking branch) for git pull and git push

前端 未结 5 1945
情深已故
情深已故 2020-11-30 22:59

Is there a way to set up a git repository, so that git pull defaults to one remote and git push defaults to another? I know I can set both

相关标签:
5条回答
  • 2020-11-30 23:16

    user392887's answer is mostly correct, but:

    1. You should prefer to use SSH. According to GitHub, "We strongly recommend using an SSH connection when interacting with GitHub. SSH keys are a way to identify trusted computers, without involving passwords."

    2. Anyone using RHEL/CentOS 6 will be using git 1.7.1 by default, which supports set-url.

    So, the preferred solution for git 1.7.1. and later is:

    git remote set-url --push origin git@github.com:username/somerepo.git
    
    0 讨论(0)
  • 2020-11-30 23:20

    For Git 1.6.4 and later, set remote.<name>.pushurl with git config.

    One might use this to pull using the read-only https: protocol and push using an ssh-based protocol.


    Say origin's url (remote.origin.url) is https://git.example.com/some/repo.git. It is read-only, but you have write access through the ssh-based ‘URL’ git@git.example.com:some/repo.git. Run the following command to effect pushing over the ssh-based protocol:

    git config remote.origin.pushurl git@git.example.com:some/repo.git
    
    0 讨论(0)
  • 2020-11-30 23:29

    Since Git 1.8.3, you can use the remote.pushDefault option to do exactly what you want (i.e. having different default remotes for pull and push). You can set the option just like any other; for example, to set it to the pushTarget remote, use

    git config remote.pushDefault pushTarget
    

    This option will have the following effect:

    • git pull will pull from the remote specified by the remote option in the relevant branch section in .git/config, while
    • git push will push to the remote specified by remote.pushDefault.

    Note that you need to specify the name of a remote, not an URL. This makes this solution more flexible than the solution involving remote.<name>.pushurl, because (for example) you will still have tracking branches for both remotes. Whether you need or want this flexibility is up to you.

    The release notes say this option was added specifically to support triangular workflows.

    0 讨论(0)
  • 2020-11-30 23:35

    From what I can gather from the git config man page, the upstream repo is:

    • by default origin
    • set by branch.remote
    • always for both git pull/fetch and git pull

    For a given branch, I don't see any way to have two separate remote by default.

    0 讨论(0)
  • 2020-11-30 23:40

    Since Git version 1.7.0, you can set this with:

    git remote set-url --push origin https://your.push.com/blah/
    
    0 讨论(0)
提交回复
热议问题