git pull 和 git push 的不同默认远程(跟踪分支)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2916845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:19:14  来源:igfitidea点击:

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

gitbranch

提问by svick

Is there a way to set up a git repository, so that git pulldefaults to one remote and git pushdefaults to another? I know I can set bothby changing the value of the remotevariable in branch section of .git/config, but how to do it for each direction separately?

有没有办法设置 git 存储库,以便git pull默认为一个远程并git push默认为另一个?我知道我可以通过更改 的分支部分中变量的值来设置两者,但是如何分别为每个方向进行设置?remote.git/config

采纳答案by Chris Johnsen

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

对于 Git 1.6.4 及更高版本,remote.<name>.pushurl使用git config 进行设置。

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

人们可能会使用它来使用只读https:协议进行拉取,并使用基于 ssh 的协议进行推送。



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' [email protected]:some/repo.git. Run the following command to effect pushing over the ssh-based protocol:

Sayorigin的 url( remote.origin.url) 是https://git.example.com/some/repo.git. 它是只读的,但您可以通过基于 ssh 的 'URL' 进行写访问[email protected]:some/repo.git。运行以下命令以实现基于 ssh 的协议的推送:

git config remote.origin.pushurl [email protected]:some/repo.git

回答by user392887

Since Git version 1.7.0, you can set this with:

从 Git 版本 1.7.0 开始,您可以使用以下命令进行设置:

git remote set-url --push origin https://your.push.com/blah/

回答by MvanGeest

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

由于Git的1.8.3,您可以使用remote.pushDefault选项做你想要什么(即具有不同的默认遥控器的pullpush)。您可以像其他任何选项一样设置该选项;例如,要将其设置为pushTarget远程,请使用

git config remote.pushDefault pushTarget

This option will have the following effect:

此选项将产生以下效果:

  • git pullwill pull from the remote specified by the remoteoption in the relevant branch section in .git/config, while
  • git pushwill push to the remote specified by remote.pushDefault.
  • git pull将从remote中相关分支部分中的选项指定的远程拉取.git/config,而
  • git push将推送到指定的遥控器remote.pushDefault

Note that you need to specify the nameof 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.

请注意,您需要指定远程的名称,而不是 URL。这使得该解决方案比涉及 的解决方案更灵活remote.<name>.pushurl,因为(例如)您仍然会有两个遥控器的跟踪分支。您是否需要或想要这种灵活性取决于您。

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

发行说明说这个选项是专门为了支持三角工作流而添加的。

回答by VonC

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

从我可以从git config man page收集到的,上游 repo 是:

  • by default origin
  • set by branch.remote
  • always for both git pull/fetchand git pull
  • 默认原点
  • 通过设置 branch.remote
  • 总是为git pull/fetchgit pull

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

对于给定的分支,我看不到默认情况下有两个单独的远程任何方法。

回答by potto

user392887's answeris mostly correct, but:

user392887 的回答大部分是正确的,但是:

  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.

  1. 您应该更喜欢使用 SSH。根据 GitHub 的说法,“我们强烈建议在与 GitHub 交互时使用 SSH 连接。SSH 密钥是一种识别受信任计算机的方法,而不涉及密码。”

  2. 任何使用 RHEL/CentOS 6 的人都会默认使用 git 1.7.1,它支持set-url.

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

因此,git 1.7.1 的首选解决方案。后来是:

git remote set-url --push origin [email protected]:username/somerepo.git