Mirroring from GitLab to GitHub
Asked Answered
G

6

80

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

Gentlemanatarms answered 15/5, 2015 at 20:53 Comment(5)
Are you planning on keeping your Gitlab instance or completely moving to Github?Leghorn
possible duplicate of How to move git repository with all branches from bitbucket to github?Eustacia
possible duplicate of Transfer git repositories from GitLab to GitHub - can we, how to and pitfalls (if any)?Elba
@MattiVirkkunen Yes. See my comment on below answer.Gentlemanatarms
for the opposite question (mirroring from github to gitlab) look here: serverfault.com/questions/531931/mirror-github-to-gitlab/849151Thralldom
T
79

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)

Thralldom answered 5/6, 2019 at 18:21 Comment(8)
Just tried it with private repo on both sides and it worked for me. 👍Hickson
GitLab has more detailed instructions including using a token instead of a password in their docsKenay
is it possible to mirror the issues/wiki in addition to the code?Constantia
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."Woad
@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/' Kistler
Can anybody confirm if this still works after mandatory 2-factor-authentification is enabled in github?Kistler
It works with access-tokens. You must set the access-token as password.Cupo
@Kistler - 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.Autoionization
B
22

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

Broach answered 19/10, 2019 at 16:21 Comment(2)
will this show the previous git contributions on GitHub too?Montero
I'd imagine so as it will push all commits, but I'd give it a try and find out to be sure.Broach
P
17

If you prefer SSH connection over HTTPS connection for GitLab to GitHub mirroring:

  • Copy SSH clone URL from your GitHub repository. (e.g. [email protected]:yourusername/yourrepositoryname.git)
  • In GitLab, go to Settings > Repository > Mirroring repositories. in your repository page.
  • Input repository URL. Don't directly paste copied link! Modify it like ssh://[email protected]/yourusername/yourrepositoryname.git
  • Select Authentication method as SSH public key.
  • Click Mirror repository.
  • After adding, click "Copy SSH public key" button next to your repository link. enter image description here
  • In GitHub, go to your target repository page and navigate Settings > Deploy keys, click to Add deploy key button and paste your public SSH key which was generated by GitLab.
  • Don't forget to check Allow write access checkbox as GitLab will require write access to mirror.

Reference

Penile answered 15/9, 2022 at 8:52 Comment(3)
After hours of trying I thought this feature was broken. No one else mentions that SSH key needs to be added as a deploy key (per-project level), not as a regular SSH-key (per user-level).Proton
Thanks so much, finally a guide that makes sense to me!Spitzer
Note the changes made in the SSH URL if you have bad eyes like I do: 1. Add ssh:// at the beginning, 2. Replace the colon with a slash.Uphroe
R
15

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

Riga answered 15/5, 2015 at 21:15 Comment(2)
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.Gentlemanatarms
@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: #14288788 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. #21963372Riga
T
5

Post Aug 13, 2021 Use of just username/password for mirroring repo from GitLab to GitHub will fail because we need to use a personal access token (PAT) to do this.

Step 1: Create a 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: https://<<githubtoken>>@github.com/<<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.
Terminable answered 3/6, 2022 at 10:59 Comment(3)
What is the minimum required scope for the GitHub Personal Access Token?Kimble
How do I do this with organization repo? I tried all answers here and was wondering if you knewNummulite
@Nummulite I believe you should have a role as "Maintainer" of the repo in gitlab to do this.Terminable
H
1

If there are too many repos on your gitlab, it's too difficult to migrate or mirror those repos.

I was in the same situation before, to solve this, I wrote an easy tool to mirror all you visible repos between gitlab and github. For example, it will mirror-clone all you visible repos on gitlab, create repos on your github account, and mirror-push them.

Gitee is not supported now, you could see tests.go to reliaze how to use it.

https://github.com/kom0055/git-mirror

I'm writing a more specific readme.

Hailey answered 3/7, 2023 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.