git push to multiple repositories simultaneously
Solution 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.
Solution 2
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
Kissaki
Updated on July 08, 2022Comments
-
Kissaki almost 2 years
How can I make
git push
to push not only toorigin
but also another remote repository?as
git push
is only an alias forgit 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.
-
Kissaki over 13 yearsThe remote “hack” is probably what I was looking for. (It’s not redefining the
git push
->git push origin HEAD
alias but does the thing I want it to.) In.git/config
once can add multipleurl
s to aremote
. Thanks. -
ripper234 about 11 yearsSomething isn't working for me: stackoverflow.com/questions/15273040/…
-
MatthewLee about 10 yearsThank you for this answer. I just set this up and it is probably worth noting that I needed to leave out the ssh:// in the urls (I'm using bitbucket and bluehost) so my git config command looked like this: git config --add remote.all.url user@server/repos/g0.git
-
Paul Verschoor almost 10 yearsadditional info: the first
git config -add
will create a fetch & push remote, while the subsequentgit config -add
will only create a push remote. -
Derick Alangi over 7 yearsGreat one there @g19fanatic. It works like charm :)
-
Саша Черных about 7 yearsI'm sorry, how Windows users may use your automatic script? Thanks.
-
Édouard Lopez about 7 years@СашаЧерных try Bash on Windows
-
Саша Черных about 7 years@ÉdouardLopez: I have Windows 10 LTSB, at the moment Bash on Windows don't support LTSB. Thanks.
-
Shafi over 6 yearsWhat if the two repo has different password for same username (email)?
-
g19fanatic over 6 years@MASh You can usually setup ssh keys to solve that issue.
-
Latency almost 4 yearsThat just asks me if I want to create a new branch on the remote with the name I am using for "all".