How to automatically mirror a Gitlab project to Github

To add the post-receive hook, you must follow the steps......

Sometimes you need to auto-sync Gitlab repos to Github, so here's what you have to do.

We need to add a post-receive hook in Gitlab's repo, on the Gitlab's server, which will trigger every time you do a push to the Gitlab's repo.

To add the post-receive hook, you must follow the steps:

A. On the Gitlab server:

su git
bash
ssh-keygen
cat ~/.ssh/id_rsa.pub

Add this pub key to Github, in here: https://github.com/Organization/RepoName/settings/keys, with write access.

B. On the Gitlab server, you should now be able to:

ssh -T git@github.com

C. On the Gitlab server:

cd ~/git-data/repositories/Organization/RepoName.git
git remote add github git@github.com:Organization/RepoName.git
mkdir custom_hooks
cd custom_hooks
nano post-receive

add the following in the post-receive file:

#!/usr/bin/env bash
git push --mirror github

then write to file, exit, and:

chmod +x post-receive

That's about everything, let us know if you have questions.

Want more awesome stuff?

stay up to date with our articles by signing up to our newsletter