How to migrate GitHub repository to GitLab?

import Image from ‘@components/Image.astro’

Gitlab and GitHub are cloud hosting services for managing git repositories. It provides private and public repositories with more features like CI and CD functionalities Sometimes, You have a repository in Github. you want to migrate the git repository to GitLab.

This tutorial explains two ways to migrate to the GitLab repository.

You have access to github.com and gitlab.com with credentials

How to migrate the repository from GitHub to GitLab?

A first approach using the User interface

  • First, log in to gitlab.com by giving the credentials

  • Once user logged in, Header navigation click on + icon and select `Create New Project/Repository

  • Select Import project on the Create Project screen as a given screenshot

Gitlabe create project import
  • Select Repository By URL as given below

  • Import Project with repository URL
  • It is redirected to the following page

Gitlab project configuration details

and enter the following details

  • Git Repository URL: This is a public git repository, If it is a private repository, First you need to authenticate with git with the below user and password details

  • username and password: These are optional and can be used when the git repository is private

  • Project Name: Gitlab project

  • Next, Create a Project

  • Once Import is done, You have a GitLab project created with code

  • It imports all branches, and commits the history of files and configurations from the git repository

Command line to transfer Gitlab to GitHub repository

Following are steps with the command line using the git command

  • First, create a Proect in GitLab
  • Goto dashboards🔗
  • Click on New project
  • Create a Blank project and provide the following details
    • Project name: node-unittest
    • slug: node-unittest
    • Next, Create a project
  • It creates an empty project in Gitlab

Next, Open the Terminal

  • create a directory using mkdir nodetest command
  • Run the below commands to clone git repository
git clone --mirror https://github.com/username/nodetest
  • Change directory using cd nodetest.git
cd nodetest.git
  • chnage remote repository to gitlab(newly created repository)
git remote add gitlab http://gitlab.com/username/nodetest.git

Next, push changes with all commits, and branches into GitLab with the below command

git push gitlab --mirror

if the above commands succeed, It imports the project successfully.

If the above command throws the below error

To gitlab.com:username/nodetest.git
! [remote rejected] main (pre-receive hook declined)
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'gitlab.com:username/nodetest.git'

Please run the set of below commands

git remote remove origin
git remote add origin http://gitlab.com/username/nodetest.git
git fetch --all
git push -u origin --all