Introduction

Since I am running a self-hosted Git server, using the powerful software Gitea, and want to use travis-ci.org for CI I need to mirror my repositories to GitHub because Travis does not support self-hosted Git. Since I couldn't find an easy and short documentation on how to do this I decided to hack this myself.

Here I show how I did this very easily.

Prerequisites

  • A Git repository under /home/git/hanez/helloworld managed by user "git"
  • A HTTP(S) interface for accesing your repository from GitHub like https://git.unixpeople.org/hanez/helloworld.git
  • A GitHub account

Import repository to your GitHub account

TODO

Become user "git" on your Git server

$ su - git

Generate a SSH key if not already done

$ ssh-keygen

Add the content of ".ssh/id_rsa.pub" to the deploy keys at GitHub

You can add the public SSH key to the repository at GitHub in the settings of each repository. In this case I use the "hanez/helloworld" repository.

Change directory to bare repository

$ cd /home/git/hanez/helloworld.git

Add "remote" to repository config

$ git remote add --mirror=push github git@github.com:hanez/helloworld.git

This will make my "config" look like this:

$ cat config
[core]
        repositoryformatversion = 0
        filemode                = true
        bare                    = true
[remote "github"]
        url = git@github.com:hanez/helloworld.git
        mirror = true

Create post-receive hook

First change directory

cd hooks/post-receive.d

Create the hook

$ cat <<EOF >> github
#!/usr/bin/env sh
git push --quiet github &
EOF

Make the hook executable

$ chmod +x github

Conclusion

You see, it is very easy to mirror your self-hosted Git repository to GitHub. With this configuration your repository will be mirrored on every push to your GitHub account. Now you can use services like Travis-CI or Docker-Hub to rebuild code or containers even when not using GitHub as your primary source code management platform. I believe this is very useful to many so I decided to publish my workflow here.

Hope this is helpful to you... ;)