• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Android
  • Computer Science
  • GitHub
  • Mac OS
  • Programming
  • How To
  • Windows
CTech

CTech

Your Guiding Technology

You are here: Home / GitHub / Managing Multiple SSH Keys for Different GitHub Repositories

Managing Multiple SSH Keys for Different GitHub Repositories

Update August 19, 2024 by CentralTechDev

When working with multiple GitHub repositories, particularly private ones, using SSH keys for secure communication is a common practice. However, if you manage several repositories with different SSH keys, you may encounter issues, especially when switching between projects. One common error you might see is:

Please make sure you have the correct access rights and the repository exists.

This error typically occurs when the SSH key you’re using doesn’t match the repository’s access credentials. In this guide, I’ll show you how to set up and manage multiple SSH keys for different GitHub repositories to avoid this problem.

Table of Contents

Toggle
  • Problem Overview
  • Step-by-Step Solution
    • 1. Generate SSH Keys
    • 2. Add the SSH Keys to the SSH Agent
    • 3. Configure the SSH Config File
    • 4. Clone or Push Using the Custom Hostnames
  • Conclusion

Problem Overview

Let’s assume you have two projects:

  • A local project named horas with a corresponding private repository on GitHub.
  • Another project named blog with its own GitHub repository.

You might have already generated an SSH key and linked it to the horas repository. However, when you create a new key for the blog repository and deploy it to GitHub, the previous key associated with horas gets overwritten. As a result, trying to push or pull from horas will cause the error mentioned above.

Step-by-Step Solution

The solution involves creating separate SSH keys for each repository and configuring your system to use the appropriate key for each project.

1. Generate SSH Keys

First, generate a unique SSH key for each repository:

For the horas project:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_horas

For the blog project:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_blog

The -f option specifies the file name for the SSH key. This ensures that each project has its own distinct SSH key.

2. Add the SSH Keys to the SSH Agent

Next, you need to add these keys to the SSH agent, which manages your keys and passes the correct one when connecting to a repository:

Start the SSH agent:

eval "$(ssh-agent -s)"

Add your keys to the agent:

ssh-add ~/.ssh/id_rsa_horas
ssh-add ~/.ssh/id_rsa_blog

3. Configure the SSH Config File

To make sure the correct key is used for each repository, you need to configure the ~/.ssh/config file. This file allows you to define settings for different SSH connections.

Open or create the ~/.ssh/config file:

nano ~/.ssh/config

Add the following configuration:

Host horas
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_horas

Host blog
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_blog

This configuration tells your system to use the id_rsa_horas key when connecting to the horas repository and the id_rsa_blog key for the blog repository.

nano config key

4. Clone or Push Using the Custom Hostnames

Finally, when you clone or push to your repositories, you’ll use the custom hostnames you defined in the SSH config:

For the horas repository:

git clone git@horas:your_username/horas.git

For the blog repository:

git clone git@blog:your_username/blog.git

By following this method, each repository uses its respective SSH key, avoiding conflicts and the dreaded access rights error.

Conclusion

Managing multiple SSH keys might seem a bit tricky at first, but with proper configuration, you can seamlessly work across different GitHub repositories. This setup ensures that each project uses its dedicated SSH key, eliminating access issues and keeping your workflow smooth.

If you encounter similar issues or have further questions, feel free to share them in the comments below!

Filed Under: GitHub Tagged With: git, github, key, repository, ssh

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

More to See

How to Fix the “You Shut Down Your Computer Because of a Problem” Error on macOS

September 5, 2024 By CentralTechDev

missing file lightroom

How to Relink Missing or Offline Files in Adobe Lightroom Classic: A Step-by-Step Guide

August 21, 2024 By CentralTechDev

Tags

adobe android battery cloud cpanel creative domain files git github hosting key lightroom macos mds_store missing problem relink repository search shutdown spotlight ssh tutorial

Footer

Text Widget

This is an example of a text widget which can be used to describe a particular service. You can also use other widgets in this location.

Examples of widgets that can be placed here in the footer are a calendar, latest tweets, recent comments, recent posts, search form, tag cloud or more.

Sample Link.

Recent

  • Connect cPanel Git Version to GitHub Private Repository
  • How to Fix the “You Shut Down Your Computer Because of a Problem” Error on macOS
  • How to Relink Missing or Offline Files in Adobe Lightroom Classic: A Step-by-Step Guide
  • 30 Key Terms and Concepts in Git and GitHub: A Developer’s Guide
  • What is Git and GitHub? A Comprehensive Guide

Search

Tags

adobe android battery cloud cpanel creative domain files git github hosting key lightroom macos mds_store missing problem relink repository search shutdown spotlight ssh tutorial

Copyright © 2025 · CentralTech.Dev