Image of an opened up HDD

Backing up your GitLab repositories to GitHub

A guide on mirroring your GitLab repositories to GitHub, mostly because I keep forgetting how to exactly do it

TLDR

GitLab allows you to easily back up your git repositories using the mirror function, in this case GitHub, which has some quirks to set up.

Why I want to back up my GitLab repos?

I like to use GitLab and GitHub for my coding projects. I prefer GitLab for my private repositories, both due to the easy project management tools and GitLab CI.

GitHub comes into play when I want to host public repositories, it’s better for visibility, is in general nicer for public repositories, and people are used to GitHub for public projects.

Now some of my private projects are actually important, and I’d like to keep them somewhat safe (I might actually start also backing them up to a self-hosted git instance) by storing them also on GitHub. The added benefit of mirroring my repositories is that the activity widget on my profile also contains my work on GitLab. But that’s more of a dopamine boost for myself than anything else.

Setting up GitLab mirroring to GitHub

Setting up mirroring for GitLab to GitHub has some quirks. Quirks that I tend to forget every time I’m setting up mirroring again. So that’s why I finally decided on creating a step-by-step guide, instead of me floundering around for half an hour trying to piece together what I had to do again.

Step 1: Setting up your GitHub mirror repository

This is easy enough, create a new GitHub repo with the title and the description you like.

Step 2: Getting your GitHub deploy key

To allow GitLab to access our GitHub repository we want to use a ‘deploy key’. The benefits of a deploy key over a PAT (Personal access Token), is that they don’t expire, and only give the rights to this particular repository.

You can find the keys at your URL: https://github.com/{group-or-users}/{project}/settings/keys. Or you can navigate towards it from your newly created repository. Press ‘settings’, then on the left under security select ‘deploy keys’. Now add a new deploy key and keep it safe for now.

Step 3: Getting the right repository URL

If we want to sync to GitHub we want to have our URL to be a specific format: ssh://ssh.github.com:443/{group-or-user}/{project}.git

This allows us to hit the correct endpoint from GitLab on GitHub’s side.

Step 4: Setup mirroring on GitLab

Now we are done with GitHub for now, time to get back to GitLab to the repository that you want to mirror.

Select ‘settings’ on the right and click on ‘Repository’, now on this page we want to expand the section ‘Mirroring repositories’. Now we are going to add a new mirrored repository.

We fetch our repository URL we constructed in the previous section as our ‘Git repository URL’. Now for mirror direction, this might be greyed out, but we want to select ‘Push’. For authentication we use ‘Username and Password’, the username is ‘git’, and the password is the deploy key we stored.

Now you can select to keep divergent refs, in case you’re planning to push to your GitHub repo as well, which I wouldn’t advise.

And if you want to only push protected branches (you’ll miss out on your activities though) select that too.

This should be it and now you have successfully backed-up your repository on GitLab.

Comments