Git: Pushing Commits to Multiple Remote Repositories

Image: Git push to multiple repositories

It’s possible in practise to have multiple remote repositories for a single git project. This is a useful technique that every savvy programmer should use when dealing with highly distributed applications.

Let’s say you wish to push to the following two repositories simultaneously:

git://repo1/repo.git
git://repo2/repo.git

You first have to provision two pushurls for each remote repositories by issuing these add commands:

git remote set-url --add push origin git://repo1/repo.git
git remote set-url --add push origin git://repo2/repo.git

So when you wish to push to the two repos at the same time, you simply call:

git push origin

For a better understanding, let us clone a repository for a different technique and display all the fetch and push urls provisioned:

git clone git://repo1/git
git remote -v
--> origin git://repo1/repo.git (fetch)
--> origin git://repo1/repo.git (push)
git config -l | grep '^remote\.'
--> remote.origin.url=git://original/repo.git
--> remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

Let us set up a new remote called all  as follows, this will basically provision a fetch and push url :

git remote add all git://repo1/repo.git
git remote -v
--> all git://repo1/repo.git (fetch)
--> all git://repo1/repo.git (push)
--> origin git://repo1/repo.git (fetch)
--> origin git://repo1/repo.git (push)
git config -l | grep '^remote\.all'
--> remote.origin.url=git://repo1/repo.git
--> remote.origin.fetch=+refs/heads/*:refs/remotes/all/*

The commands above provisions the all remote, listing out its details using the verbose, -v flag and config -l flag. Next add a pushurl to the all remote, pointing to the repo2 repo:

git remote set-url add --push all git://repo2/repo.git
git remove -v
--> all git://repo1/repo.git (fetch)
--> all git://repo2/repo.git (push)
--> origin git://repo1/repo.git (fetch)
--> origin git://repo1/repo.git (push)
git config -l | grep '^remote\.all'
--> remote.all.url=git://repo1/repo.git
--> remote.all.fetch=+refs/heads/*:refs/remotes/all
--> remote.all.pushurl=git://repo2/repo.git

Let us now add a pushurl for the repo1 repo:

git remote set-url --add --push all git://repo1/repo.git
git remote -v
--> all git://repo1/repo.git (fetch)
--> all git://repo2/repo.git (push)
--> all git://repo1/repo.git (push)
--> origin git://repo1/repo.git (fetch)
--> origin git://repo1/repo.git (push)
git config -l | grep '^remote\.all'
--> remote.all.url=git://repo1/repo.git
--> remote.all.fetch=+refs/heads/*:refs/remotes/all
--> remote.all.pushurl=git://repo2/repo.git
--> remote.all.pushurl=git://repo1/repo.git

So the two pushurl have been added as seen and as a test you can push the master branch to all repos, by simply calling this command:

git push all master

Tip 1: For Git version 2.15.1 or lesser omit the –-add flag while provisioning the pushurls for it to work.

Author: daltonwhyte

A technocrat who believes in a smart future, that will be proliferated with systems that allow us to focus on the bigger picture.

Leave a Reply

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