One of the things that recent github outages have highlighted is that although git is designed to be decentralized version control system, many people are not treating it that way. Rather than having multiple remotes, people push to the same place and generally rely too much on github. I’ve seen people build their whole infrastructure around github, including deploy scripts for their sites and install scripts for new developers. Furthermore, dependency managers such as Composer & Cocoapods (not intending to single anyone out) rely on github to pull in dependencies. If github goes down, you can’t fix your dependencies or pull in new ones. In an ideal world these dependency managers should have built in support for mirrors, but I’m not aware of any such feature at the time of writing.
What I’m going to illustrate is how easy it is to set up your git remotes so you push to two places. If github goes down, it doesn’t matter, assuming everyone on your team follows this guide the code will also be on bitbucket, or wherever else you choose. But this tutorial will mirror to github & bitbucket. Let’s begin.
-
Firstly, I’m going to assume you have a remote set up already for github named “origin”. Rename it to “github:
git remote rename origin github
-
Create a new repository on bitbucket and name it the same as the one on github.
-
Add it as a remote using the instructions provided. But make sure you call it “bitbucket” and not “origin”. Something like so:
git remote add bitbucket ssh://[email protected]/username/somerepo.git git push -u bitbucket --all
-
Next type:
git config -e
This will open up a text editor and allow you to edit your git configuration.
-
We need to add the urls for every remote so far (github and bitbucket) into a new remote called “origin”. It should look something like this:
[remote "origin"] url = [email protected]:username/somerepo.git url = ssh://[email protected]/username/somerepo.git
-
Next time you run
git push origin
it will push to both
And that’s it. It’s as simple as that.
Recent of versions of git allow you to add multiple places per remote like this using git commands, but I prefer to see exactly what is going on in the configuration.
**Update #1: **reddit user MatmaRex makes a very valid point to be careful renaming remotes if you have any remote branches. I’ve updated the instructions to reflect this.