能用一个命令推送到所有git遥控器吗?

bal*_*ton 183 git

而不是做:

git push origin --all && git push nodester --all && git push duostack --all
Run Code Online (Sandbox Code Playgroud)

有没有办法只用一个命令来做到这一点?

谢谢 :)

Ari*_*zis 273

创建一个all包含多个repo URL 的远程名称:

git remote add all origin-host:path/proj.git
git remote set-url --add all nodester-host:path/proj.git
git remote set-url --add all duostack-host:path/proj.git
Run Code Online (Sandbox Code Playgroud)

然后就是git push all --all.


这是它的样子.git/config:

  [remote "all"]
  url = origin-host:path/proj.git
  url = nodester-host:path/proj.git
  url = duostack-host:path/proj.git
Run Code Online (Sandbox Code Playgroud)

  • 这种方法的一个问题是你必须在`all`遥控器可用时添加新的URL,而`git remote | xargs -L1 git push --all`将自动获取任何新的遥控器. (13认同)
  • Torvalds先生(Git的创建者)提到他使用这种方法,但他声称这仅仅是为了方便而且没有技术优势http://marc.info/?l=git&m=116231242118202&w=2"最后甚至推送到多个存储库的"git push all"实际上最终会为每个存储库连接一次,所以它实际上只是做多个"git push"的简写.没有真正的技术优势,只是方便." (7认同)
  • 为什么会是一个错误? (4认同)
  • 超酷的技巧!唯一的缺点是它不会移动远程头部.你需要在执行这样的推送后立即运行`git fetch --all`. (4认同)
  • 提示:为了在发送提交时不需要输入 `all`,只需使用“origin”而不是“all”:`git remote set-url --add origin nodester-host:path/proj.git` (4认同)

wea*_*ish 223

将所有分支推送到所有遥控器:

git remote | xargs -L1 git push --all
Run Code Online (Sandbox Code Playgroud)

或者,如果您想将特定分支推送到所有远程控制器:

替换master为您要推送的分支.

git remote | xargs -L1 -I R git push R master
Run Code Online (Sandbox Code Playgroud)

(Bonus)为命令创建一个git别名:

git config --global alias.pushall '!git remote | xargs -L1 git push --all'
Run Code Online (Sandbox Code Playgroud)

Running git pushall现在将所有分支推送到所有远程.

  • Git允许您将该调用转换为自定义命令.只需将它放在1)在你的路径上的文件中,2)你有执行权限,3)称为"git- [自定义名称]"(例如git-foo,git-push-all)你会能够简单地输入"git [custom name]"(例如git foo,git push-all). (3认同)
  • @Tony 在 Ubuntu 上,`man xargs` 表示选项 `-l` 已被弃用,因为它不在 POISX 规范中。 (3认同)
  • 非常好的和简单的解决方案。顺便说一句,您可以使用 xargs -l 而不是 -L 1,-l 选项与 -L 1 相同。此外,有时我将 --all 添加到 git push 中。git 远程 | xargs -l git push --all (2认同)
  • 我在OSX上得到了xargs:非法选项-l。想通了,您需要`git remote |。xargs -L1 git push` (2认同)
  • @kyb在git别名语法中,“!”表示以下内容不是内部git命令,而是外部shell命令。 (2认同)

Men*_* Lu 71

如果你想总是推送到repo1,repo2和repo3,但总是只从repo1拉,请设置远程'origin'为

[remote "origin"]
    url = https://exampleuser@example.com/path/to/repo1
    pushurl = https://exampleuser@example.com/path/to/repo1
    pushurl = https://exampleuser@example.com/path/to/repo2
    pushurl = https://exampleuser@example.com/path/to/repo3
    fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

在命令行配置:

$ git remote add origin https://exampleuser@example.com/path/to/repo1
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo1
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo2
$ git remote set-url --push --add origin https://exampleuser@example.com/path/to/repo3
Run Code Online (Sandbox Code Playgroud)

如果你只是想从拉repo1却推到repo1repo2 特定分支specialBranch:

[remote "origin"]
    url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[remote "specialRemote"]
    url = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    pushurl = ssh://git@aaa.xxx.com:7999/yyy/repo1.git
    pushurl = ssh://git@aaa.xxx.com:7999/yyy/repo2.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[branch "specialBranch"]
    remote = origin
    pushRemote = specialRemote
    ...
Run Code Online (Sandbox Code Playgroud)

请参阅https://git-scm.com/docs/git-config#git-config-branchltnamegtremote.

  • 我不确定为什么没有更多的选票.这实际上非常方便,因为它允许你只做一个没有任何参数的`git push`. (4认同)
  • 请为我投票! (2认同)
  • 在我看来,这似乎更合适。应该有最高票数。 (2认同)

小智 17

作为CLI的替代编辑.git/config文件,您可以使用以下命令:

# git remote add all origin-host:path/proj.git
# git remote set-url --add all nodester-host:path/proj.git
# git remote set-url --add all duostack-host:path/proj.git
Run Code Online (Sandbox Code Playgroud)

这同样git push all --all适用于此.

你已经完成了与答案#1相同的完成.您刚刚使用命令行而不是原始编辑配置文件.