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


Answers:


252

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

git remote | xargs -L1 git push --all

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

master您要推送的分支替换。

git remote | xargs -L1 -I R git push R master

(奖励)为命令添加git别名:

git config --global alias.pushall '!git remote | xargs -L1 git push --all'

git pushall现在,运行将把所有分支推到所有遥控器。


1
非常好的和简单的解决方案。顺便说一句,您可以使用xargs -l代替-L 1,-l选项与-L 1相同。此外,有时我在git push中添加--all。git远程| xargs -l git push --all
Tony,

2
xargs: illegal option -- l上OSX。想通了,您需要git remote | xargs -L1 git push
balupton

4
Git允许您将该调用转换为自定义命令。只需将其放在以下文件中:1)在您的路径上,2)您具有执行权限,3)称为“ git- [自定义名称]”(例如git-foo,git-push-all),您将只需输入“ git [custom name]”(例如git foo,git push-all)即可。
安德鲁·马丁

2
@Tony在Ubuntu上man xargs-l,不赞成使用该选项,因为它不在POISX规范中。
wjandrea

2
@kyb在git别名语法中,!意味着以下内容不是内部git命令,而是外部shell命令。
弱化了

285

创建一个all具有多个回购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

然后就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

5
超级酷的把戏!唯一的缺点是它不会移动远程磁头。git fetch --all完成这样的推送后,您需要立即运行。
madhead 2013年

8
Torvalds先生(Git的创建者)提到他使用了这种方法,但是他说这只是为了方便,没有任何技术优势marc.info/?l=git&m=116231242118202&w=2 “最后,甚至是“推送到多个存储库的“ git push all”实际上最终将为每个存储库连接一次,因此,它实际上只是进行多个“ git push”的简写。没有真正的技术优势,只是方便。”
马特

14
这种方法的一个问题是,您必须在新URL all可用时向其添加新URL ,而新URL git remote | xargs -L1 git push --all将自动选择任何新的Remote 。
拉菲·哈查杜安

2
提示:all每次发送提交时都不需要键入,只需使用“ origin”而不是“ all”即可:git remote set-url --add origin nodester-host:path/proj.git
Macabeus 2015年

忘记设置推送网址,否则git push将不会更新所有网址。回答相应更新
user3338098

88

如果要始终推送到repo1,repo2和repo3,但始终只从repo1中提取,则将远程“源”设置为

[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/*

在命令行中配置:

$ 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

如果你只是想从拉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
    ...

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


4
我不确定为什么没有更多的选票。这实际上非常方便,因为它允许您执行git push不带任何参数的a。
赫斯基

1
请为我投票!
孟路

1
在我看来,这似乎是更合适的方法。应该有最高的选票。
艾哈迈德(Ahmad)

这是非常有用的。是否可以将pushurl仅限于master分支?
fbucek

17

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

# 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

相同 git push all --all在这里也可以。

您已经完成了与答案1相同的操作。您刚刚使用命令行完成了此操作,而不是原始编辑配置文件。


2

我编写了一个简短的bash函数,可以在一个呼叫中推送到许多遥控器。您可以指定一个远程作为参数,多个远程之间用空格隔开,也可以不指定任何一个将其推送到所有远程。

可以将其添加到您的.bashrc或.bash_profile中。

function GitPush {
  REMOTES=$@

  # If no remotes were passed in, push to all remotes.
  if [[ -z "$REMOTES" ]]; then
    REM=`git remote`

    # Break the remotes into an array
    REMOTES=$(echo $REM | tr " " "\n")
  fi

  # Iterate through the array, pushing to each remote
  for R in $REMOTES; do
    echo "Pushing to $R..."
    git push $R
  done
}

示例:假设您的仓库有3个遥控器:rem1,rem2和rem3。

# Pushes to rem1
GitPush rem1

# Pushes to rem1 and rem2
GitPush rem1 rem2

# Pushes to rem1, rem2 and rem3
GitPush

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.