How to maintain a GitLab and GitHub account at the same time?

How to maintain a GitLab and GitHub account at the same time?

Some years ago (by an impulsive decision 😸) I moved to GitLab. I like to have my learning materials, pet projects there, but I've realised I'd like to expose them on GitHub as well.

Could I just download it as a zip folder? Yes. It'd be straightforward, but I don't want to do that - I'd loose the history, commit messages of my repo on GitHub.

Now, as developers, we don't like to repeat ourselves... so if you are in my shoes, the way to do this is mirroring your repositories from GitLab to GitHub - here's a quick way to do it, keeping code consistent both places.

The example folder of mine on GitLab which I'd like to mirror is called Udacity.

GitHub

  1. Create a repository on Github. I chose the same name as my GitLab folder: Udacity
  2. Create a GitHub personal access token:
  • In the upper-right corner of any page, click your profile photo, then click Settings.
  • In the left sidebar, click Developer settings.
  • In the left sidebar, click Personal access tokens.
  • Click Generate new token.
  • Give your token a descriptive name. e.g.: vivi-gitlab-token
  • Select the scopes, or permissions, you'd like to grant this token, make sure to have the public_repo box checked.
  • Click Generate token.
  • Click to copy the token to your clipboard and save it. (Keep in mind that for security reasons, after you navigate off the page, you will not be able to see the token again.)

GitLab

Under Udacity, Settings, go to Repository, then expand Mirroring repositories section, where you'll have to fill out this form:

image.png

as the following:

  • Git repository URL is going to be your GitHub repo URL in this format: https://<your_github_username>@github.com/<your_github_group>/<your_github_project>.git
  • Mirror direction is going to be Push according to our use case. This means that whenever I push to my GitLab repo, branches, tags and commits will be synced (pushed) to GitHub automatically.
  • Authentication method is going to default to Password.
  • Fill in Password field with your GitHub personal access token you generated as per above (GitHub section point 2).
  • Finally, click the Mirror repository button.

The mirrored repository will be listed. For example, https://*****:*****@github.com/<your_github_group>/<your_github_project>.git. If you were successful, whenever you push to GitLab, Last update attempt should be updated, too:

image.png

Now check out your synced GitHub repo! It's this easy 😉

Resources