Git 同时推送到多个存储库

如何使 git push不仅推到 origin,而且还推到另一个远程存储库?

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

我在这里不是寻找一个非 git 脚本,但是希望在 git 中为我的本地存储库设置这个脚本。

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

48785 次浏览

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.

No manual editing

You can add multiple URLs to a remote branch (e.g. all) directly from command line by using git config --add remote.xyz.url with different URLs:

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 want to copy/paste URLs 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

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.