Skip to content

Instantly share code, notes, and snippets.

@leogopal
Created September 14, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leogopal/9320b24a2f0e07d3704f714312402cdc to your computer and use it in GitHub Desktop.
Save leogopal/9320b24a2f0e07d3704f714312402cdc to your computer and use it in GitHub Desktop.
Using Multiple Git Repositories

Option 1: Multiple Remotes Pushed (And Fetched) Independently

$ cd myproject
$ git remote add bitbucket ssh://git@bitbucket.org/user/myproject.git
$ git push bitbucket master

Every time we commit any changes, we need to push to both our original “origin” and our new remote “bitbucket”:

$ git push origin master
$ git push bitbucket master

You may add this as an alias to make things easier.

alias gpob="git push origin master && git push bitbucket master"

Option 2: Single Remote With Multiple URLs Pushed (And Fetched) Consecutively

$ cd myproject
$ git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git
$ git push origin master
Everything up-to-date
Everything up-to-date

Of course silver lining has a cloud, and in this case, it is that while we can push to multiple URLs simultaneously, we can only fetch from the original origin (you can change this, but that is out of scope for this post).

Tip

To show all remotes use: $ git remote -v show

Output should look something like this:

bitbucket	git@bitbucket.org:user/myproject.git (fetch)
bitbucket	git@bitbucket.org:user/myproject.git (push)
origin	git@github.com:user/myproject.git (fetch)
origin	git@github.com:user/myproject.git (push)
origin	git@bitbucket.org:user/myproject.git (push)

Hattip to @ahmadawais for writing a tutorial on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment