GitLab is an open core Git-repository hosting service, just like GitHub. It is used by big organizations and institutions like GNOME, KDE, Standford, and many more. GitLab comes in two different distributions: Community Edition (CE) and Entreprise Edition (EE). The former is free and open source, and the latter is built on top of GitLab CE, but is source-available and proprietary, therefore following the “open core” model. GitLab EE comes with more features than GitLab CE, as well as corporate support; it is free in price, just like GitLab CE, but you can upgrade to a paid tier to gain access to even more features. You can read more about GitLab CE and EE in the official website.
Needless to say, GitLab has a neat feature called “push mirror,” which lets you push repositories to other Git hosting services, such as GitHub, Gitea, etc. At the moment, I mirror some of my repositories to my Codeberg account, a Gitea instance, from GitLab. GitLab EE takes the mirroring a step further by allowing you to pull from repositories outside of the instance, called “pull mirrors”.
I’m going to go through the simplest methods of mirroring from and to repositories using GitLab.
There are three different methods to setup push mirrors: via HTTP or HTTPS protocols, via SSH protocol, and via Git protocol. In this guide, I will only go over the first and the second methods.
The easiest way to push repositories is via HTTP or HTTPS. However, if you use GitHub, I recommend using the second method, via SSH and continuing from there, because GitHub recently deprecated password-based authentication (HTTP/HTTPS).
Alright, so here’s what you need to do:
Settings
> Repository
.Mirroring repositories
.Mirror direction
to Push
.Git repository URL
.Mirror repository
button. The page will refresh, and you will see an entry inside Mirrored repositories
.To confirm that you set up the push mirror correctly, press on the refresh button in the entry and check that it pushes without any error. If there is no error, double check that you set it up correctly by visiting the mirror’s link and checking whether it has fetched the latest commits or not.
Note: you might need to check the Mirror only protected branches
box when setting up the push mirror to get it working as intended.
The second and more secure method is to push via SSH. The only downsides to this are that it can be annoying and time consuming to setup and it can potentially become hard to manage the SSH keys if you have to set a lot of push mirrors.
With that out of the way, here’s what you need to do:
Settings
> Repository
.Mirroring repositories
.Mirror direction
to Push
.ssh://git@<host>/<group>/<repo>.git
, where <host>/<group>/<repo>.git
is the link of your repository, for example ssh://git@gitlab.com/TheEvilSkeleton/theevilskeleton.gitlab.io.git
. Unfortunately, SCP style (git@example.com:group/repo.git
) isn’t supported.Mirror repository
button. The page will refresh, and you will see an entry inside Mirrored repositories
.Mirrored repositories
, this will copy the SSH public key.Settings
> SSH and GPG keys
.New SSH key
.Just like the first method, to confirm that you have set up the push mirror correctly, press on the refresh button in the entry and check that it pushes without any error. If there is no error, double check that you set it up correctly by visiting the mirror’s link and checking whether it has fetched the latest commits or not.
Note: you might need to check the Mirror only protected branches
box when setting up the push mirror to get it working as intended.
Unfortunately, pulling from other repositories is a GitLab Entreprise Edition only feature. The only instance that I know of with EE installed is the one managed by GitLab Inc.: gitlab.com.
Every hour, GitLab will attempt to pull from the wanted repository automatically and push it to your GitLab repository.
There are two methods to set up pull mirrors: importing a project, and manually setting it up from an existing project.
The first method is by simply importing the project because it does everything for you. As of time of writing this post, here is what you need to do:
≡ Menu
at the top left of GitLab.Projects
.Create new project
.Import project
.Repo by URL
.Git repository URL
.Mirror repository
box.Create project
.It’s that easy. To confirm that you have set up the pull mirror correctly, go to Settings
> Repository
, expand Mirroring repositories
and check if your repository is mirrored.
If you have an already imported repository, then here is what you need to do:
Settings
> Repository
.Mirroring repositories
.Git repository URL
.Mirror direction
is set to Pull
.Mirror repository
button. The page will refresh, and you will see an entry inside Mirrored repositories
.To confirm that you have set up the mirror correctly, press on the refresh button in the entry and check that it pulls without any error.
GitLab mirrors are really convenient since you don’t have to use the command-line to either push or pull. GitLab Community Edition allows you to setup push mirrors, and GitLab Entreprise Edition additionally lets you setup pull mirrors.
Needless to say, there are many other ways to mirror repositories using GitLab. The ones mentioned above are the simplest and most intuitive methods. If you want to read the technical methods, see the official documentation. Enjoy mirroring!
Source file: 2021-07-26-mirror-gitlab-repositories.md