git git同时推送到多个存储库

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

git push to multiple repositories simultaneously

gitversion-controldvcs

提问by Kissaki

How can I make git pushto push not only to originbut also another remote repository?

我怎样才能git push不仅推送到origin另一个远程存储库?

as git pushis only an alias for git push origin, can I alias git push to push to 2 remote repositories at once (with just that one command)?

由于git push只是 的别名git push origin,我可以将 git push别名为一次推送到 2 个远程存储库吗(仅使用一个命令)?

I'm not looking for a non-git script here but would like to set this up for my local repository in git.

我不是在这里寻找非 git 脚本,而是想在 git 中为我的本地存储库设置它。

When I tried it with post-push scripts I failed.

当我尝试使用 post-push 脚本时,我失败了。

回答by g19fanatic

I don't think you can do it just by setting a flag on git, but you can modify a config file that will allow you to push to multiple remote repositories without manually typing them all in (well only typing them in the first time and not after)

我认为你不能仅仅通过在 git 上设置一个标志来做到这一点,但是你可以修改一个配置文件,该文件将允许你推送到多个远程存储库,而无需手动全部输入(以及只在第一次输入它们)不是之后)

In the .git/configfile you can add multiple urls to a defined remote:

.git/config文件中,您可以将多个 url 添加到定义的远程:

[remote "all"]
    url=ssh://user@server/repos/g0.git
    url=ssh://user@server/repos/g1.git

If you git push allnow you push to all the remote urls.

如果您git push all现在推送到所有远程网址。

回答by édouard Lopez

No manual editing

无需手动编辑

You can add multiple URL to a remote branch (e.g. all) directly from command line by using git config --add remote.xyz.urlwith differents URL:

您可以all使用git config --add remote.xyz.url不同的 URL直接从命令行添加多个 URL 到远程分支(例如):

git config --add remote.all.url ssh://user@server/repos/g0.git
git config --add remote.all.url ssh://user@server/repos/g1.git

Fully automatic

全自动

If you're super lazyand don't bear to copy/paster URL several times, this is for you:

如果您非常懒惰并且不忍多次复制/粘贴 URL,那么这适合您:

function git-add-push-all() {
  while read -r name url method; do
    git config --add remote.all.url "$url"
  done < <(git remote -v | awk '!/^all/ && /push/')
}

git-add-push-all # from git (sub)directory

A full bashy script is possible (test $nameand $method), but awkis sweet and there is love for everyone.

一个完整的 bashy 脚本是可能的(测试$name$method),但它awk是甜蜜的,每个人都喜欢。

Push

Then you can push to all remote with

然后你可以推送到所有远程

git push all

References

参考

回答by Pierre-Olivier Vares

You can also get url from configured remotes :

您还可以从配置的遥控器获取 url:

for repo in g0 g1 ...
do
    git config --add remote.all.url `git config remote.$repo.url`
done

where g0, g1, ... are the names of your remotes.

其中 g0, g1, ... 是您的遥控器的名称。