I have 45 projects/repositories on Gitlab. I want to move it to my Github.
I know there is a way to do it on the UI one by one by import repository.
But how to do it all at once?
Here is a python based solution assuming we are referencing only repos inside your projects list (not within sub-groups, though this can be modified to work for that case).
These steps will walk you through the hard part of this problem, getting the repositories to exist on GitHub (with added bonus of copying descriptions over and matching the public/private settings). To do this, you will have to use the GitHub API or create them manually.
Prereqs:
This will use the GitLab list projects GET request. Also, it will repeatedly use the GitHub create repo POST request. Once you run this (personally tested), you can use the list of repos output at the end and write some simple bash to iterate over them cloning each repository, adding a git remote, then pushing to the remote. Alternatively, you could use the --mirror
option above. Regardless, options abound at this point.
GITLAB_USERNAME = "<user>"
GITHUB_USERNAME = "<user>"
GITLAB_TOKEN = "<token>"
GITHUB_TOKEN = "<token>"
import requests
import json
# get the list of repos from gitlab
x = requests.get( \
url='https://gitlab.com/api/v4/users/{user}/projects'.format(user=GITLAB_USERNAME), \
headers={'PRIVATE-TOKEN': GITLAB_TOKEN})
# print the response code (200 is success)
print(x)
repo_names = []
# iterate the repos 1 by 1
for repo in json.loads(x.content):
# print out repo details to see functioning
print('Name:', repo['name'])
print('Description:', repo['description'])
print('Visibility:', repo['visibility'])
print('ssh_url_to_repo:', repo['ssh_url_to_repo'])
repo_names.append(repo['name'])
# create the repo on GitHub with the name, description, and private/public
y = requests.post( \
url='https://api.github.com/user/repos', \
headers={'Accept': 'application/vnd.github+json', 'Authorization': 'token {token}'.format(token=GITHUB_TOKEN)}, \
json={'name': repo['name'], 'description': repo['description'], 'private': repo['visibility']=='private'})
# print the response code (201 is created [success])
print(y)
print('')
# print the list of repos for reference
for name in repo_names:
print(name)