Git Push to multiple repositories simultaneously

Posted by Matt Bryson on 24-Jan-2013 12:11:17
Find me on:

Whilst working on our touch swipe project, I have been working with 2 remotes; the public gitHub repo, and our company repo.

With Git, this is super simple, just add another remote and give it a name and the url:

git remote add repo_name git@server.com:company/project/repo.git

With multiple remotes, you need to push to each remote individually, but if you simply want 2 remotes to be a mirror of each other there is a way to push to both of them at the same time.

First add your remotes as above, and then edit the git config for your project.

git config -e

You are now in vi and can edit the config file. You will see something like this, where I have my original origin pointing to our work repo, and my added gitHub repo.

[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@server.com:company/project/repo.git
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "github"]
url = git@github.com:mattbryson/TouchSwipe-Jquery-Plugin.git
fetch = +refs/heads/*:refs/remotes/github/

What you need to do is rename the existing remote origin, and update the fetch property to point to the new name. Then create a new remote origin that points to both of our remotes, so it looks like this (notice 'origin' has become 'work' for the first remote name as well as the url in the fetch property).

[remote "work"]
fetch = +refs/heads/*:refs/remotes/work/*
url = git@server.com:company/project/repo.git
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "github"]
url = git@github.com:mattbryson/TouchSwipe-Jquery-Plugin.git
fetch = +refs/heads/*:refs/remotes/github/
[remote "origin"]
url = git@github.com:mattbryson/TouchSwipe-Jquery-Plugin.git
url = git@server.com:company/project/repo.git
    

Now when you run

git push

You will commit to both remotes at the same time.

m.

Topics: GIT

About the author

Matt Bryson is an experienced systems engineer with over 15 years Development and Systems Operations experience.

NEW - Free eBook

Transforming your eDetail process. Click below to download the eBook.

New Call-to-action

Subscribe to Email Updates