GitHub

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:

  1. Go to “Settings” → “Repository” and expand “Mirroring repositories”
  2. Click “Add new”
  3. Paste the GitHub URL from earlier into “Git repository URL”
  4. Edit the URL to replace https at the start with ssh; in the Omnipedia example, the URL would now be ssh://github.com/neurocracy/omnipedia.git
  5. Now click “Detect host keys” and wait for it to finish - this is important
  6. Set “Authentication method” to “SSH public key”
  7. Enter “git” as the user name
  8. Click “Mirror repository”
  9. 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:

  1. Go to “Settings” → “Deploy keys”
  2. Click “Add deploy key”
  3. Give your key a title; I recommend something like “GitLab mirror key”
  4. Paste the SSH public key you copied from GitLab previously
  5. Check “Allow write access” - this is important
  6. Click “Add key”

Testing

Mirroring should now be fully configured, so you can test it in one of two ways:

  1. 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.
  2. 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”.

GitHub and GitLab commit patches

Bet you didn’t know that both GitHub and GitLab can provide you with a patch file by appending “.patch” to the end of the commit URL? I sure didn’t. For example, this commit:

https://gitlab.com/Ambient.Impact/drupal-modules/commit/6b4b2e808cb842de83c97a722b804e1ca88a2e7f

becomes:

https://gitlab.com/Ambient.Impact/drupal-modules/commit/6b4b2e808cb842de83c97a722b804e1ca88a2e7f.patch

This is super useful when you need to use a patch for a Composer package, etc.