Sync subversion to github.com

June 27, 2012

programming

This is a post that details on how to sync a subversion repository to git repository on github.com, and how to keep it in sync.

The following sites were instrumental in getting this to work:

There are a number of steps to take. From a bird’s eye view:

  1. Use git svn to clone the svn repo to a git repo;
  2. Create a github git repo;
  3. Add a remote origin in your local git to the remote github repo;
  4. Use some SSH foo to use a separate SSH key for pushing to github.com.

Prerequisites

  • We are working with the fictional svn repo located at https://svn.example.net/example;
  • git-svn is installed;
  • ssh is installed.

Clone the repository

mkdir github.com
cd github.com
git svn clone -s https://svn.example.net/example

Create github repo

Go to the github.com’s website and create a new repository, in this case we’ll use my username (miekg). So the github.com’s repository is:

github.com:miekg/example.git

Add a remote in (local) git

git remote add origin git@github.com:miekg/example.git

And push the contents to the remote (i.e. github):

git push origin master

And voila. You have a git repository from a cloned svn repository.

Keeping it in sync

Ideally you want to run a cronjob to keep this repository in sync with the subversion repository. The git commands used are trivial:

git svn rebase
git push

However the ssh key you’re using has a passphrase. An idea is to use a separate key (without a passphrase) just for this repository. Sadly it is not possible to restrict a ssh key to a specific repository, so this new, passphrase-less key can be used to update all your repositories.

But still this can be useful, so how do you set this up?

  1. First create a new key pair: ssh-keygen -f github.com.
  2. Create a Host-stanza in ~/.ssh/config:

    Host example.github.com
        HostName github.com
        User git
        IdentityFile ~/.ssh/github.com
    
  3. Use the “fake” host (example.github.com) as the remote branch in git:

    git remote add origin example.github.com:miekg/example.git
    

    If you already added the remote you can just edit .git/config and make the change there.

  4. Any git push will now use the ssh-server example.github.com, the user git and the identity file ~/.ssh/github.com and will not prompt for a passphrase.

Dealing with changes/patches

The hard part is getting changes made in this git repository back upstream (in the origin subversion repository). You can do this with git, but it gets complicated (IMHO).

A simpler idea is just to not touch this git repository, but fork it, make your changes there and send the patch to the maintainer of the subversion repository. The git repository then gets synchronized again (hopefully with your changes).

Misc