git push to multiple repositories simultaneously [duplicate]

a 夏天 提交于 2019-11-26 03:48:11

问题


How can I make git push to push not only to origin but also another remote repository?

as git push is 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)?

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

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


回答1:


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)

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

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

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




回答2:


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.url with differents 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 lazy and don't bear to copy/paster URL several times, this is for you:

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 $name and $method), but awk is sweet and there is love for everyone.

Push

Then you can push to all remote with

git push all

References

  • git-config(1) Manual Page



回答3:


You can also get url from configured remotes :

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.



来源:https://stackoverflow.com/questions/4255865/git-push-to-multiple-repositories-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!