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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5785549/
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-10 10:45:54  来源:igfitidea点击:

Able to push to all git remotes with the one command?

git

提问by balupton

Instead of doing:

而不是做:

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

Is there a way to do that with just one command?

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

Thanks :)

谢谢 :)

回答by Aristotle Pagaltzis

Create an allremote with several repo URLs to its name:

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

Then just git push all --all.

那么就git push all --all.



This is how it looks in .git/config:

这是它的外观.git/config

  [remote "all"]
  url = origin-host:path/proj.git
  url = nodester-host:path/proj.git
  url = duostack-host:path/proj.git

回答by weakish

To push all branches to all remotes:

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

git remote | xargs -L1 git push --all

Or if you want to push a specific branch to all remotes:

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

Replace masterwith the branch you want to push.

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

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

(Bonus) To make a git alias for the command:

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

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

Running git pushallwill now push all branches to all remotes.

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

回答by Meng Lu

If you want to always push to repo1, repo2, and repo3 but always pull only from repo1, set up the remote 'origin' as

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

[remote "origin"]
    url = https://[email protected]/path/to/repo1
    pushurl = https://[email protected]/path/to/repo1
    pushurl = https://[email protected]/path/to/repo2
    pushurl = https://[email protected]/path/to/repo3
    fetch = +refs/heads/*:refs/remotes/origin/*

Configure at command line:

在命令行配置:

$ git remote add origin https://[email protected]/path/to/repo1
$ git remote set-url --push --add origin https://[email protected]/path/to/repo1
$ git remote set-url --push --add origin https://[email protected]/path/to/repo2
$ git remote set-url --push --add origin https://[email protected]/path/to/repo3

If you onlywant to pull from repo1but push to repo1and repo2for a specific branch specialBranch:

如果你只是想从拉repo1,但推repo1repo2特定分支specialBranch

[remote "origin"]
    url = ssh://[email protected]:7999/yyy/repo1.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[remote "specialRemote"]
    url = ssh://[email protected]:7999/yyy/repo1.git
    pushurl = ssh://[email protected]:7999/yyy/repo1.git
    pushurl = ssh://[email protected]:7999/yyy/repo2.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[branch "specialBranch"]
    remote = origin
    pushRemote = specialRemote
    ...

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

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

回答by Kurt Vanderwater

As a CLI Alternative to editing the .git/config file, you could use the following commands:

作为编辑 .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

The same git push all --allworks here as well.

同样的git push all --all工作在这里。

You have accomplished the same as answer #1. You have just done it with Command Line instead of raw editing of the config file.

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

回答by KeyboardCowboy

I wrote a short bash function to push to many remotes in one call. You can specify a single remote as a parameter, multiple remotes separated by spaces or don't specify any to have it push to all remotes.

我编写了一个简短的 bash 函数,可以在一次调用中推送到多个遥控器。您可以指定单个遥控器作为参数,多个遥控器以空格分隔,或者不指定任何遥控器以将其推送到所有遥控器。

This can be added to your .bashrc or .bash_profile.

这可以添加到您的 .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
}

Example: Let's say your repo has 3 remotes: rem1, rem2 and rem3.

示例:假设您的存储库有 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 Vitaly Zdanevich

You can utilize git hooks- especially pre-push: add non-origin pushes to .git/hooks/pre-push.

您可以使用git hooks- 特别是pre-push:将非源推送添加到.git/hooks/pre-push.