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

时间:2011-04-26 03:19:50

标签: git

而不是:

git push origin --all && git push nodester --all && git push duostack --all

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

谢谢:)

5 个答案:

答案 0 :(得分:255)

创建一个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

然后只是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

答案 1 :(得分:184)

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

git remote | xargs -L1 git push --all

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

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

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

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

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

现在运行git pushall将所有分支推送到所有远程。

答案 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

同样的git push all --all也适用于此。

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

答案 3 :(得分: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

答案 4 :(得分:0)

您可以利用git hooks-尤其是pre-push:向.git/hooks/pre-push添加非来源推送。

相关问题