How to avoid relying on github: mirror your repository

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.

  1. 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
  2. Create a new repository on bitbucket and name it the same as the one on github.
  3. 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://git@bitbucket.org/username/somerepo.git
    git push -u bitbucket --all
  4. Next type:
    git config -e

    This will open up a text editor and allow you to edit your git configuration.

  5. 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 = git@github.com:username/somerepo.git
        url = ssh://git@bitbucket.org/username/somerepo.git
  6. 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.

 

Be Sociable, Share!

    11 thoughts on “How to avoid relying on github: mirror your repository

    1. Adam

      Hey,

      When you pay for a service, you should expect the site to provide the service. I like your article, just don’t feel I should have 10 engineers sitting around, not able to do peer reviews, etc. which even though your mirroring, doesn’t fix.

    2. deanclatworthy

      Hi Adam. I tend to agree, but it’s up to us at developers to work around and anticipate single points of failure.

    3. Chris Pall

      So this pushes to both places at the same time correct? What if I just want to push to one place? How do I that?

    4. ChrisBlah

      “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.”

      Composer supports repositories other than Packagist. You can set up your own private package repository (e.g. using Satis), in fact that’s one of the primary use cases Composer was built for!

      Please read this: http://getcomposer.org/doc/05-repositories.md#hosting-your-own

    5. deanclatworthy

      I’m aware of the fact composer supports alternative sources other than packagist, but what if that goes down? It should ideally support mirrors so if one source is down, it tries another.

    6. Blueeee

      I’ve started using both bitbucket and github in this fashion recently, but I never knew you could then have a remote which pushes to both URLs. Thanks!

    7. Mxx

      If current public GitHub is unacceptable for you, buy GitHub Enterprise and run your own 100% uptime system.
      If you can’t or don’t want to do that, then you’ll just have to expect and accept downtime.

    8. Pingback: sync projects to github and bitbucket! | SirVon Andre Thomas

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>