Connect Multiple Git Repositories for a Single Project

Using Git is really important when working on a project. In some cases, we may need multiple git repositories for a single project. Yes, we can give access to multiple users in a single repository but what if you are not permitted to do so? Here in this article, we will discuss the steps to connect multiple Git repositories for a single project.

Prerequisites

Before starting this article, I Assume that the reader has a basic knowledge of Git technology. I also believe that the reader has previously used the platforms like GitHub, BitBucket, GitLab, etc.

What we will learn

Here in this article, we will learn the following things:-

  • Create a simple project
  • Set up a GitHub account and push the project to it.
  • Create another GitHub repository, and push the same project to it

Create a simple project

Let us create a project directory named simple-html-project. Now create a file index.html inside it and add the below code.

<html>
    <body>
        Hello world
    </body>
</html>

We can use code editors such as VS code to do these steps. I am not explaining this step more.

Opening the file in a browser will return the text “Hello world” as below.

So we have created a simple project on our local system.

Create a GitHub repository and add our project to it.

I have already written an article for creating a new GitHub repository and adding our project to it. You have to refer to this.

GitHub has recently replaced the use of passwords with a token to push code to the repository.

To create a personal access token from our GitHub account ( Settings -> Developer settings -> Personal access tokens) with full access rights.

Use this token instead of a username and password to push the code.

Note that, here we are adding the repository URL to our project and pushing our code to the origin master using the commands below.

git remote add origin [your_remote_repository_url]
git push -u origin master

We are adding the origin of our first GitHub repository here. This is very important in our article and discuss later.

So we have successfully added our project to a GitHub repository. Now we need to add the same project to another GitHub repository in another account.

Create a second GitHub account

In the above same manner, create a second GitHub account. We will use this account later but to create an SSH key, we need the Email address of the second GitHub account.

Generate a new SSH key and add it to the SSH-AGENT

We need to access the second repository to add our project to it. We need to create an SSH key from our local system and add it to the SSH-AGENT.

Note that the steps for this will vary with the operating system.

If you are using operating systems such as Windows or Linux, the article Generating a new SSH key and adding it to the ssh-agent will guide you to set up.

Here I am explaining the steps for the macOS.

Execute the below command with the Email address of the GitHub account of the second repository.

ssh-keygen -t ed25519 -C "your_email@example.com"

This creates a new SSH key, using the provided email as a label.

Generating public/private algorithm key pair

When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.

Enter a file in which to save the key (/Users/you/.ssh/id_algorithm): [Press enter]

At the prompt, type a secure passphrase.

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Now let us add the SSH key to the ssh-agent. For this continue with the below steps.

Start the ssh-agent in the background.

eval "$(ssh-agent -s)"

This will return the message shown below.

Agent pid 59566

Create a file config and add the code into it and save. We can use the nano editor to complete this step.

sudo nano ~/.ssh/config

This will open up the nano editor. Copy and paste the below code into it and save it.

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

If you are not familiar with the nano editor, use the below keys to save the file and close the nano editor.

Control + o
<Enter>
Control + x

Now execute the below command to add our SSH key in the SSH-AGENT on our local system and store our passphrase in the keychain.

ssh-add -K ~/.ssh/id_ed25519

Add the SSH key generated to our second repository

Click on the SSH and GPG keys tab and add generated SSH key here.

After adding the new SSH key, you can see the page below.

Add the remote URL to our project and push the code to the second repository

From the root directory of our project, execute the below commands to add the new remote repository.

git remote add origin2 [your_remote_repository_url]
git push origin2 development:master

Note that you need to use the SSH remote URL in this case.

This pushes the code from our first repository’s development branch to the master branch of the second repository.

Here we are actually adding a second origin named origin2 (Remote origin) to our project and pushing the code to it.

Summary

So in this article, we created a simple project, and add it to a GitHub repository. We also pushed the same project to another GitHub repository using the SSH key permission. In the same manner, we can push our project to multiple git repositories.

One thought on “Connect Multiple Git Repositories for a Single Project

  1. Hi,

    We are looking to create an app for our website:
    vitoconcierge.com

    looking forward.

    Best wishes,
    Vito

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.