I’m a big fan of self-hosting my stuff. That’s why all the Git repositories where I keep the code I’m writing and any other things I want to have versioned live in a Gitolite instance on a rented server.
However for various reasons (not having to provide public git access on my own server, Travis CI, ease of collaboration via pull requests) it still makes sense to mirror the open source stuff on Github.
Obviously it’s cumbersome to manually push to two remotes all the time, so I set out to automate this. Here’s how.
On the Gitolite server
There’s a config file named
/home/git (or whatever your Gitolite user’s home dir is). Here you need to set the LOCAL_CODE variable (search for it in th file, there’s two suggested settings already and you just need to pick one):
LOCAL_CODE there’s two possiblities - you can either manage the custom code directly on the server or through the
gitolite-admin repository. As you can see the latter is the route I went with.
AUTH_OPTIONS line originally wasn’t present in my config file. It determines what ssh options are set for keys managed by gitolite in
/home/git/.ssh/authorized_keys and defaults to
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty. We remove the part disabling agent forwarding so our hook script can use the ssh keys from your local machine to connect to Github.
Also make sure the
repo-specific-hooks are enabled in the
ENABLE array further down in the same file. Thanks to Lucas Camargo who pointed that out in his comment!
After making these modifications you have to update the gitolite installation:
In your gitolite-admin repo
Add a hook script
Create a directory
local/hooks/repo-specific and in there a simple shell script that mirrors a given repository to github:
Don’t forget to replace jkraemer with your own Github username .
If you set the
LOCAL_CODE variable above to something outside the
gitolite-admin repo, you have to create the hook script in that location.
Configure which repositories to mirror
I have my gitolite config organized by declaring variables (groups of repositories and users) on the top, and rules using these variables further down below. This way it’s easy to make changes without having to hunt around for the right places.
So for the mirroring, I have a group of repositories to mirror declared at the top, and a rule setting the mirror script for these repositories later on:
Commit all changes (don’t forget to
git add the hook script) and push them to your server.
That’s it! Now everytime something is pushed to a repository in your
@mirrored-repos list, the hook script will be called and do a push to github.