Push to Multiple Git Repos (Posted on December 22nd, 2012)

Here's a cool little trick I found out about the other day. Even though Git is a distributed revision control system, there are times when you need to push to two systems at once. In my case those systems are Github and Heroku. Here is a simple way to do it by modifying your .git/config file.

        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "heroku"]
        url = [email protected]:maxburstein.git
        fetch = +refs/heads/*:refs/remotes/heroku/*
[remote "github"]
        url = [email protected]:mburst/burstolio.git
        fetch = +refs/heads/*:refs/remotes/github/*
[remote "origin"]
        url = [email protected]:maxburstein.git
        url = [email protected]:mburst/burstolio.git

The "heroku" and "github" remotes are generated by git and are created in the setup instructional steps of each service respectively. I went ahead and manually added the remote "origin" and just copied the url variable from the other remotes. You'll now be able to push to both by calling "git push origin" and then you can fetch from each one as individually needed.

Tags: Git


  • Lucas - 2 years, 3 months ago

    I "figured this out" recently. I stopped using it because pushing to "origin" won't update your remote head for "heroku" and "github". Having some semantic-client-side connections for this could be tricky, so I've just learned to avoid it. (But if you need to do multiple pushed often, and know enough about git to handle the caveats, this works well.)


  • nsf - 2 years, 3 months ago

    I just do this: [alias] pub = "!git push pubgit && git push github"


  • Gergely Polonkai - 2 years, 3 months ago

    Why modify the config directly if git can do it for you? $ git remote add heroku [email protected]:maxburstein.git $ git remote add github [email protected]:mburst/burstolio.git


  • Max Burstein - 2 years, 3 months ago

    That will work perfectly for setting up the heroku and github branch. However, for pushing to both of them I needed to add a 3rd branch manually that I called origin. If you try to git remote add a url to a branch that already exists it will throw back "fatal: remote heroku already exists". So to get around that I just went ahead and did it manually.