We have configured a project in IntelliJ as well as a remote GitLab repository, where we maintain the project - we commit and push our code to that repository. Now we would like to configure another GitLab repository, to which we could commit and push the same code. This repository could be, for example, used to overview the code without requiring an access to the original repository or having an option to change the code in that repository. In this article we'll explain how we can achieve this by using IntelliJ.

1. Adding a second remote repository

Let’s assume we’re working on the code from repository1, already imported to IntelliJ.

Add another remote repository, we’ll call it repository2.

  1. In the menu, go to VCS→ Git → Remotes…
    git remotes
  2. Copy the link of the repository2 by going to your GitLab, selecting your project, clicking on Clone, and copying the URL.
    gitlab clone from repo
  3. Click on a plus, name the remote (eg. origin-copy) and provide the link to the repository2. Click OK.
    git define new remote
  4. You should now see the new repository – repository2 added to the list of Git remotes.
    git list git remotes
  5. In the menu, go to VSC → Git → Fetch. This way you make sure you have access to all the repository2 branches.

1.1. Alternative: using the terminal

Move to your project folder and type the following:

git remote add origin-copy This email address is being protected from spambots. You need JavaScript enabled to view it.
	:user/repository2.git

2. Git commit and push

Push the current repository1 branch to repository2 branch:

  1. Press Ctrl+shift+k (or go to VSC → Git → Push…)
  2. Select the repository you’d like to push your code to. For example, if your repository1 is named origin and repository2 is named origin-copy, select origin-copy. After that you can also select the branch you’d like to push your code to by clicking on the name of the branch (it will autocomplete the name of the branch as you type it in). You can also type in a name of a new branch, which will be then automatically created in the repository2.
    git push commits select remote
  3. When you’re done with selecting the repository (we named repository2 origin-copy) and the branch we want to push to (in our case develop) you can press the button Push.
    git push commits origin copy

2.1. Alternative: using the terminal

In the CLI type:

git push -u origin-copy develop

3. Creating a new remote repository

In case the repository2 does not exist yet, make sure you create a new project in GitLab by going to https://gitlab.yourdomain.com/projects/new (note: don't forget to substitute yourdomain with the name of your domain).

You can also follow the step by step visual guide below:

  1. Go to your GitLab (in our case the URL is https://gitlab.xenovation.com/) and click on New project.
    git new project gitlab

  2. Write a name of your project in Project name field and click on Create project button.
    git create new project details
  3. Your project will be created and you will be redirected to the project repository details view.
    git new project created

4. Summary

In this article we learned how to push identical code to two different repositories in GitLab by using IntelliJ, following step by step instructions.

To find out how to delete or revert a bad remote commit, follow the article How to revert a git commit
Follow this to learn How to change remote git repository