Mirroring GitLab to GitHub: A Step-by-Step Guide with Public Key Authentication
Tags: git, github, bash, automation
Published: 2023-03-02 06:22:55

## Introduction
GitLab and GitHub are two of the most popular code hosting platforms used by developers around the world. Sometimes, you may need to mirror a GitLab repository to a GitHub repository for backup or collaboration purposes. Manually doing this can be time-consuming, especially if you have multiple repositories to mirror. Fortunately, you can automate the process using a shell script. In this article, we will show you how to do this with a simple example shell script.

## Prerequisites

Before we begin, you need to have the following:

- A GitLab account with a repository that you want to mirror to GitHub.

- A GitHub account with a repository where you want to mirror the GitLab repository.

- Git installed on your local machine.

- SSH keys set up and added to your GitLab and GitHub accounts for authentication.


### Step 1: Set up the Shell Script
To mirror a GitLab repository to a GitHub repository, we will use a shell script. Here is an example script that you can use:

```bash
#!/bin/bash

# Define an array that maps GitLab and GitHub repository URLs
declare -A repo_map=(
    ["https://gitlab.com/username/repo.git"]="https://github.com/username/repo.git"
    ["https://gitlab.com/username/other-repo.git"]="https://github.com/username/other-repo.git"
)

# Loop through the array and mirror each GitLab repository to its corresponding GitHub repository
for gitlab_url in "${!repo_map[@]}"; do
    github_url="${repo_map[$gitlab_url]}"
    repo_name="$(basename "$gitlab_url" .git)"
 
    # Clone the GitLab repository
    git clone --mirror "$gitlab_url"
 
    # Change to the local repository directory
    cd "$repo_name.git"
 
    # Add the GitHub repository as a remote
    git remote add github "$github_url"
 
    # Push all branches and tags to the GitHub repository
    git push --mirror github
 
    # Remove the local repository directory
    cd ..
    rm -rf "$repo_name.git"
done

```


This script defines an associative array `repo_map` that maps GitLab and GitHub repository URLs. The loop then clones each GitLab repository using `git clone --mirror`, adds the GitHub repository as a remote using git remote add, and pushes all branches and tags to the GitHub repository using `git push --mirror`.

Make sure to replace the GitLab and GitHub repository URLs in the `repo_map` array with the URLs for your own repositories.

Save this script to a file, for example, `mirror.sh`.



### Step 2: Make the Shell Script Executable

Next, we need to make the shell script executable. Run the following command in the terminal:

```bash
chmod +x mirror.sh
```
This command gives the script execution permission.


### Step 3: Run the Shell Script

Now we can run the shell script to mirror the GitLab repository to the GitHub repository. To do this, run the following command:

```bash
./mirror.sh
```


This command runs the `mirror.sh` script and starts the mirroring process.

The script will loop through each GitLab repository in the repo_map array, clone it, add the GitHub repository as a remote, and push all branches and tags to the GitHub repository.

Once the script has finished running, all of your GitLab repositories should be mirrored to your GitHub repositories.

### Conclusion

Mirroring GitLab repositories to GitHub repositories can be a tedious process, especially if you have multiple repositories to mirror


#### Notes:
In addition to GitLab and GitHub, this technique can be used to mirror repositories from other code hosting platforms such as Bitbucket, AWS CodeCommit, and others. You simply need to modify the script to use the correct URLs for the repositories that you want to mirror.

To mirror repositories from other providers, you will need to modify the `repo_map` array in the script to use the correct URLs for each repository. For example, if you want to mirror a Bitbucket repository to a GitHub repository, you would replace the GitLab and GitHub URLs in the `repo_map` array with the Bitbucket and GitHub URLs respectively.

Additionally, you may need to modify the SSH keys that you use for authentication depending on the code hosting platform that you are mirroring from and to. However, the process of setting up SSH keys is similar for most code hosting platforms and can usually be done through the user settings or profile pages.

In conclusion, this shell script can be a powerful tool for automating the process of mirroring repositories between different code hosting platforms, saving you time and effort. With a little bit of modification, you can use this technique to mirror any provider's repositories, including GitLab, GitHub, Bitbucket, AWS CodeCommit, and more.