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

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

18190 次浏览

Git 配置手册页的数据来看,上游回购是:

  • 默认来源
  • set by branch.remote
  • 始终为 git pull/fetchgit pull

对于给定的分支,我看不出有任何办法可以有两个独立的远程 默认情况下

对于 Git 1.6.4及更高版本,将 remote.<name>.pushurl设置为 Git config

可以使用这种方法使用只读 https:协议进行拉取,并使用基于 ssh 的协议进行推送。


假设 origin的 url (remote.origin.url)是 https://git.example.com/some/repo.git。它是只读的,但是您可以通过基于 ssh 的“ URL”git@git.example.com:some/repo.git进行写访问。运行下面的命令来实现对基于 ssh 的协议的推送:

git config remote.origin.pushurl git@git.example.com:some/repo.git

从 Git 版本1.7.0开始,您可以通过以下方式设置:

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

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.

因此,git1.7.1和更高版本的首选解决方案是:

git remote set-url --push origin git@github.com:username/somerepo.git

从 Git 1.8.3开始,您可以使用 remote.pushDefault选项来完成您想要的任务(即为 pullpush使用不同的默认远程控制器)。您可以像设置其他选项一样设置该选项; 例如,要将其设置为 pushTarget远程,请使用

git config remote.pushDefault pushTarget

这一选择将产生以下效果:

  • git pull将从 .git/config中相关分支部分的 remote选项指定的远程提取,而
  • git push将推到由 remote.pushDefault指定的远程。

注意,您需要指定远程的 姓名,而不是 URL。这使得此解决方案比涉及 remote.<name>.pushurl的解决方案更加灵活,因为(例如)您仍然可以对两个远程进行跟踪分支。您是否需要或想要这种灵活性取决于您自己。

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

如果你也来这里寻找每个分支的解决方案,这里是手册:

branch.<name>.pushRemote

When on branch , it overrides branch..remote for pushing. It also overrides remote.pushDefault for pushing from branch . When you pull from one place (e.g. your upstream) and push to another place (e.g. your own publishing repository), you would want to set remote.pushDefault to specify the remote to push to for all branches, and use this option to override it for a specific branch.

感谢 MvanGeest链接到 Git 1.8.3版本说明。这些发布说明说:

  • 一个三角形的“从一个地方拉,推到另一个地方”的工作流程 更好地支持新的 remote.pushdefault(重写 和 branch.*.pushremote(重写 branch.*.remote)配置变量。

I use such a triangular workflow 一直都是 for open-source contributions. For example: I have my own GitHub fork of llvm/llvm-project, and I want to keep my own main branch up-to-date with the upstream's main. So I frequently git pull upstream main; it would be convenient if I could just type git pull instead. 但是, I don't want any chance that I might fat-finger git push<return> instead of git push origin main<return> and accidentally main0 to the upstream project's repo before I intended to! So, before today, my .git/config looked like this:

[remote "origin"]
url = git@github.com:Quuxplusone/llvm-project
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
url = git@github.com:llvm/llvm-project
fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "main"]
merge = refs/heads/main
remote = origin

根据上面引用的发布说明,我刚刚将我的本地回购的 .git/config改为:

[remote "origin"]
url = git@github.com:Quuxplusone/llvm-project
fetch = +refs/heads/*:refs/remotes/origin/*
[remote "upstream"]
url = git@github.com:llvm/llvm-project
fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "main"]
merge = refs/heads/main
remote = upstream
pushremote = origin

Now I can do a simple git checkout main ; git pull to pull from upstream/main, and a simple git checkout main ; git push to push to origin/main. This is the "triangular workflow" I want.