Mirroring from Gitlab to Github

24,917

Solution 1

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

https://gitlab.com/yourUserNameInGitLab/yourRepoName/settings/repository

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:

https://[email protected]/yourUserNameInGitHub/yourRepoName.git

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

Solution 2

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

git remote set-url --add origin [email protected]:<USERNAME>/<PROJECTNAME>.git

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

Solution 3

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 https://githost.org/extuser/repo.git

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 https://github.com/ghuser/repo.git

  2. Finally remove the local repository you made.

    cd ..

    rm -rf repo.git

Share:
24,917
Ingwie Phoenix
Author by

Ingwie Phoenix

Updated on July 27, 2022

Comments

  • Ingwie Phoenix
    Ingwie Phoenix over 1 year

    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. :)

  • Ingwie Phoenix
    Ingwie Phoenix almost 9 years
    My Gitlab server is actually sort-of public. I wanted to mirror git.ingwie.me/ingwie/bird3 into a github organization. But since feature request notices and such are there, i only wanted to mirror it to github. So I dont want to move, but to mirror. There used to be even a special icon for a mirrored repo too.
  • matrixanomaly
    matrixanomaly almost 9 years
    @IngwiePhoenix ah sorry, the way your question was phrased made lots of people think you want to transfer over. Perhaps edit to specifically say you want a mirror and keep the original? In any case refer to this: stackoverflow.com/questions/14288288/… where it mentions of the same setup, you can automate it with gitlab watching the github repo, or webhooks/ post receive hook, I think this is what you want. stackoverflow.com/questions/21962872/…
  • Ryan Bayne
    Ryan Bayne about 4 years
    Just tried it with private repo on both sides and it worked for me. 👍
  • Jess Telford
    Jess Telford about 4 years
    GitLab has more detailed instructions including using a token instead of a password in their docs
  • emre
    emre almost 4 years
    is it possible to mirror the issues/wiki in addition to the code?
  • PraveenMak
    PraveenMak about 3 years
    As per documentation, this does not work if you have LFS. From Gitlab documentation, "Git LFS objects will be synced in pull mirrors if LFS is enabled for the project. They will not be synced in push mirrors."
  • alfC
    alfC about 3 years
    @JessTelford, is that still working? after generating the token and following the instructions I get the same error: 13:close stream to gitaly-ruby: rpc error: code = Unknown desc = Gitlab::Git::CommandError: remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/alfcUser/alfcRepo.git/'
  • alfC
    alfC about 3 years
    Can anybody confirm if this still works after mandatory 2-factor-authentification is enabled in github?
  • niels
    niels almost 3 years
    It works with access-tokens. You must set the access-token as password.
  • Bill McGonigle
    Bill McGonigle about 2 years
    @alfC - my user account has 2FA configured and access tokens work. I'm pushing to a repo for my organization. PS. GitLab ought to link to this answer on their mirror configuration page! This explanation is so much better than the documentation. Thanks knocte.