Table of contents
- 1 Introduction
- 2 Prerequisites
- 3 Import repository to your GitHub account
- 4 Become user "git" on your Git server
- 5 Generate a SSH key if not already done
- 6 Change directory to bare repository
- 7 Add "remote" to repository config
- 8 Create post-receive hook
- 9 Conclusion
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.
- 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
Become user "git" on your Git server
$ su - git
Generate a SSH key if not already done
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 email@example.com:hanez/helloworld.git
This will make my "config" look like this:
$ cat config [core] repositoryformatversion = 0 filemode = true bare = true [remote "github"] url = firstname.lastname@example.org:hanez/helloworld.git mirror = true
Create post-receive hook
First change directory
Create the hook
$ cat <<EOF >> github #!/usr/bin/env sh git push --quiet github & EOF
Make the hook executable
$ chmod +x github
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... ;)