How to setup a push mirror in Gitlab

In this tutorial I will show you how you can setup a push mirror in Gitlab. This is useful when you want to push updates that your current repository receives to a remote repository. For example I use this function to backup my repositories to their counterparts on gitlab.com to reduce the risk of a data loss.

Create an Access Token for the target repository

As the repository I want to push to runs Gitlab, I will create an Access Token, so that I don’t have to provide my login credentials to my source Gitlab instance.

After logging in on the target Gitlab repository, click on the right top corner on your Avatar and then on „Edit profile“. Now in the left menu, click on „Access Tokens“. You will now be presented the Access Token page. Here input the token name, select the expiration date, the read_repository and write_repository permissions:

Gitlabs Access Token page

Now click on „Create personal access token“. Save the access token code that is now presented on the top of the page as it will not be displayed again.

If you have setup your project to auto-deploy to Maven Central, you should disable the CI/CD pipeline for your target repository to prevent trying uploading artifacts that already have been deployed.

Setup the push mirroring

Now login to the source Gitlab repository and navigate to the project you wish to setup the push mirroring for. Within the menu on the left, select „Settings“ and then click on „Repository“. Enter the target repository URL. That URL must be of the format https://$USERNAME@$REPO_URL.git, where $USERNAME is the username you wish to run the push as and $REPO_URL is the URL of the target repository. Enter your created gitlab access token into the password field:

Setting up the push mirror
If you want to save space on the target repository, select „Mirror only protected branches“.

Click on „Mirror repository“. You will now see the repository in the „Mirrored repositories“ list. Now click on the refresh button to initialy synchronize your source repository to the target repository:

Now synchronize the source repository with the target repository

If everything is setup correctly, the state of „Last successful update“ should read „just now“ or a date after the update operation finished.

Your push mirror is now working. All commits that are pushed to your source repository should now be relayed to the target repository.

Related articles