September 29, 2011

Pushing to two git remote origins from one repository

Let’s assume that you are using github for version control of some projects you want to make public. But you also want to push your repository to a second repository, maybe a private one you one on a VPS or something similar - just for backup reasons or in case you want to leave github someday.

This is easily achievable by just adding another remote, pushing to it and creating an alias for convenience. Let’s assume we want to name our secondary origin “backup”:

First create the folder and do an initial pull from github of your own repository:

dan@bart ~ $ mkdir XML-to-text-Tomboy
dan@bart ~ $ cd XML-to-text-Tomboy
dan@bart ~ $ git clone ./

Now let’s add the second remote origin and do an initial push to it:

dan@bart ~/XML-to-text-Tomboy $ git remote add backup user@server:/path/to/git/XML-to-text-Tomboy.git
dan@bart ~/XML-to-text-Tomboy $ git push backup master:master

And finally add an “all” alias for both remotes to make sure it’s easy to push to both of them every time:

dan@bart ~/XML-to-text-Tomboy vi .git/config

Add the following entry:

[remote "all"]
    url =
    url = user@server:/path/to/git/XML-to-text-Tomboy.git

Now every time you commit you can just run

dan@bart ~/XML-to-text-Tomboy $ git push all

Hope some of you have found this useful!