Clone Multiple Github Repositories With Different Deployment Keys but the Same Username

Cloning multiple GitHub repositories with different deployment keys can be useful when you need to access multiple repositories using different SSH keys associated with the same GitHub account. This guide provides step-by-step instructions on how to clone multiple repositories with different deployment keys while using the same username. By following these steps, you can streamline your workflow and manage multiple repositories more efficiently.

Step 1: Generate Deployment Keys

To begin, generate a deployment key for each repository you want to clone. Use the ssh-keygen command on your local machine to create a unique key for each repository.

Step 2: Add Deployment Keys to Your GitHub Account

Next, navigate to the “Settings” page of your GitHub account. Access the “SSH and GPG keys” section and click on the “New SSH key” button for each key. Give each key a descriptive name and paste the contents of the respective public key file into the “Key” field. Save each key to your GitHub account by clicking “Add SSH key.”

Step 3: Create a Configuration File

Using your terminal, navigate to the .ssh directory by running cd ~/.ssh/. If a configuration file doesn’t already exist, create one by running touch config. Open the configuration file with a text editor like nano using the command nano config.

Step 4: Configure the Configuration File

In the configuration file, add an entry for each repository you wish to clone. For each entry, specify the path to the private key file, the username, and the repository URL. Use the format shown below as an example:

1
2
3
4
5
6
7
8
9
Host github.com-repo1
    HostName github.com
    User git
    IdentityFile ~/.ssh/repo1_key

Host github.com-repo2
    HostName github.com
    User git
    IdentityFile ~/.ssh/repo2_key

Save the configuration file by pressing Ctrl + X, then Y, and finally Enter.

Step 5: Clone Each Repository

Navigate to the directory where you want to clone the repositories using your terminal. Run the following command for each repository, replacing username with your GitHub username and repository with the name of the repository you wish to clone:

1
2
git clone [email protected]:username/repository.git
git clone [email protected]:username/repository.git

This command will clone each repository using the respective deployment key specified in the configuration file.

Conclusion

By following these steps, you can successfully clone multiple GitHub repositories with different deployment keys but using the same username. This approach enables efficient management of multiple repositories within a single GitHub account.

0%