Mirroring a GitLab repository to GitHub
We recently migrated Neurocracy from GitHub to GitLab but I wanted to keep our numerous repositories on GitHub up to date without having to push to both. Repository mirroring is relatively simple to set up once you know the exact steps required, and it allows us to push only to GitLab yet have our GitHub repositories also receive the same commits nearly instantly.
GitLab
First, we need to set up your GitLab project. Copy your project’s GitHub HTTPS Git URL (not the SSH URL); for example, for Omnipedia it would be:
https://github.com/neurocracy/omnipedia.git
Now head to your GitLab project:
- Go to “Settings” → “Repository” and expand “Mirroring repositories”
- Click “Add new”
- Paste the GitHub URL from earlier into “Git repository URL”
- Edit the URL to replace
https
at the start withssh
; in the Omnipedia example, the URL would now bessh://github.com/neurocracy/omnipedia.git
- Now click “Detect host keys” and wait for it to finish - this is important
- Set “Authentication method” to “SSH public key”
- Enter “git” as the user name
- Click “Mirror repository”
- Lastly, click the “Copy SSH public key” button on the newly configured repository - you’ll need this for GitHub
GitHub
Now in your GitHub project:
- Go to “Settings” → “Deploy keys”
- Click “Add deploy key”
- Give your key a title; I recommend something like “GitLab mirror key”
- Paste the SSH public key you copied from GitLab previously
- Check “Allow write access” - this is important
- Click “Add key”
Testing
Mirroring should now be fully configured, so you can test it in one of two ways:
- If you have new commits locally that you haven’t pushed yet, you can push now to your GitLab project and it should mirror that to the GitHub counterpart.
- If you don’t want to add new commits, you can tell GitLab to mirror manually by clicking the “Update now” button in your GitLab project’s “Settings” → “Repository” → “Mirroring repositories”
Once you’ve done either one, you can refresh the GitHub project’s “Settings” → “Deploy keys” page, after which you should see green text under the deploy key stating something along the lines of “Last used within the last week”.