Push to multiple git remotes with one command
Synopsis
I sometimes need to push my git repository to multiple remotes — for example, when deploying to Heroku at the same time as pushing to GitHub.
Description
Adding the following to your .gitconfig
will allow you to do just that with only one command:
[alias]
pushto="!f(){ remotes=$(echo \"$1\" | tr \",\" \"\n\"); branches=$(echo \"$2\" | tr \",\" \"\n\"); for remote in $remotes; do for branch in $branches; do git push -v $remote $branch; done; done; };f"
Usage
git pushto <repositories> <branches>
Note that <repositories>
and <branches>
are both comma-delimited strings.
Examples
This following command pushes the master
branch to both the origin
and heroku
remotes:
git pushto origin,heroku master
This following command pushes the specified branches (master
and anotherbranch
) to the specified remotes (origin
and github
):
git pushto origin,github master,anotherbranch
Explanation
If you're curious, here is the prettified version of the code.
remotes=$(echo "$1" | tr "," "\n")
branches=$(echo "$2" | tr "," "\n")
for remote in $remotes; do
for branch in $branches; do
git push -v $remote $branch
done
done
Alternatives
A simpler, yet less flexible way to accomplish this would be to just add multiple URLs to one remote. For example:
git remote set-url --add origin git@github.com:example-user/example-repo.git
Then, whenever you git push origin
, git will push to both the original origin URL and the URL that you specified in the command above.