Mirroring from Gitlab to Github

I have been using a private Gitlab instance to hold all my code. But since most of the staff that work with me now have a Github account, i would really like to get moving and mirror my Gitlab repo to Github.

My situation:

  • a server running Gitlab (Omnibus)
  • a Github account for which I'll create an organization for where me and my staff can be organized together.

I know that there is the --mirror switch in git, but I am not really sure how this is ment to work. Documentation I found online was very wonky... So it would be nice if someone could help me out. :)

5

4 Answers

GitLab has now an option to do this from the UI, go to the Settings->Repository of your repo:

Then find the option "Mirror a repository" and click on expand. What you want to do is choose the "Push" mirror direction and fill this URL:

In the password field, you have to use a Personal Access Token (as GitHub has deprecated password access now), which you can generate here: (don't forget to enable the "repo" and "workflow" permissions when generating it)

8

Another options is to add an additional URL to the origin:

git remote set-url --add origin :<USERNAME>/<PROJECTNAME>.git

When you push to origin it will push to both the original origin (gitlab) and the one added above (github).

This previous StackOverflow question addresses how to move your repository from another service over to GitHub, the first answer there addresses how to do it via command line, and the second and third are more user friendly ways, which unfortunately will not work for you if your GitLab instance is on your local server (which seems to be your case).

You can however 'import' your repository from the command line to GitHub as explained by GitHub docs, this is the suggested way as GitHub offers this as an alternative to using their GitHub Importer tool (which is highlighted in that previous SO question)

A run down of steps as taken from the documentation:

  1. Create a new repository you want to push to in GitHub.
  2. Make a local bare clone from your GitLab server:

    git clone --bare

A bare clone is an exact duplicate, without a working directory for editing files, so it's a clean export.

  1. Change into that directory and then push it with the --mirror flag. The mirror flag ensures that references (branches/tags) are copied to GitHub.

    cd *repo.git*

    git push --mirror

  2. Finally remove the local repository you made.

    cd ..

    rm -rf repo.git

2

Post Aug 13, 2021 Use of just username/password for mirroring repo from Gitlab to Github will fail because we need to use PAT to do this.

Step 1: Create PAT from Github:

  • Click on your GitHub profile icon on the top right corner
  • Click Settings
  • From the menu shown on the left, click Developer Settings
  • Click Personal access tokens
  • Click Generate new token
  • Add a note that will help you identify the scope of the access token to be generated
  • Choose the Expiration period from the drop down menu (Ideally you should avoid choosing the No Expiration option)
  • Finally, select the scopes you want to grant the corresponding access to the generated access token. Make sure to select the minimum required scopes otherwise you will still have troubles performing certain Git Operations.
  • Finally click Generate Token.

You should also be able to see your personal access token. Make sure to copy it as we will need it in the following step(s).

Step 2: From Gitlab, Go to repo you wish to mirror:

  • Click on Settings.
  • Click on Repository.
  • Click on Mirror Repo option --> Expand it.
  • Use below url for Git repository URL:githubtoken>>@username>>/<<repositoryname>>.git
  • Use the token created in Step 1 to use in the part in URL above.
  • Click on Mirror Repository button without filling the password field.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like