Skip to content
Eric Bouchut edited this page Nov 3, 2013 · 25 revisions

Pushing to 2 remotes

Say I clone a repository from github.

git clone git@github.com/ebouchut/learning-git.git

Every time I ask git to push to the remote origin, I want git to push to 2 repositories github (the one I cloned from) and bitbucket (which acts a secondary/backup git repository in this case).

Here is how to do this:

git set-url --add --push origin git@github.com/ebouchut/learning-git.git
git set-url --add --push origin git@bitbucket.org/ebouchut/learning-git.git

This adds 2 push urls remote.origin.pushurl , the first one for github (git@github.com:ebouchut/learning-git.git) the second one for bitbucket (ssh://git@bitbucket.org/ebouchut/learning-git.git). Adding a remote.origin.pushurl overrides the default remote.origin.url for the push action.

git config  --local --get-regex remote.origin.\*
  remote.origin.url git@github.com:ebouchut/learning-git.git
  remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
  remote.origin.pushurl git@github.com:ebouchut/learning-git.git
  remote.origin.pushurl ssh://git@bitbucket.org/ebouchut/learning-git.git

From now using git push will push both to github and bitbucket. Now here is what happens when I do a git push. I ask git to be a bit verbose (using -v) so that it mentions where it is pushing to.

git push -v
  Pushing to git@github.com:ebouchut/learning-git.git
    To git@github.com:ebouchut/learning-git.git
    = [up to date]      master -> master
    updating local tracking ref 'refs/remotes/origin/master'
    Everything up-to-date
  Pushing to ssh://git@bitbucket.org/ebouchut/learning-git.git
    To ssh://git@bitbucket.org/ebouchut/learning-git.git
    = [up to date]      master -> master
    updating local tracking ref 'refs/remotes/origin/master'
    Everything up-to-date

Source

Clone this wiki locally