How to Use a Different SSH Key for git clone (Without Replacing Your Main Key)

Posted by _dhruv - October 21, 2025
ssh--auth

You’ve already got an SSH key (id_rsa) connected to one project — maybe your company repo or a client’s GitHub account.
Now you need to clone another private repo, but you can’t risk overwriting that main key.

So what’s the move?

Let’s walk through how to use a different SSH key for Git clone safely and cleanly — no collisions, no broken auth.

Why This Happens

When you run:

git clone git@github.com:user/repo.git

Git uses your default SSH identity stored at ~/.ssh/id_rsa.
If that key is already linked to a different GitHub or GitLab account, authentication fails or points to the wrong identity.
That’s why you shouldn’t just replace your existing key — you need to add another one and teach Git when to use it.

 

Step 1: Create a New SSH Key for the Project

Run this command (replace projectX with something meaningful):

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_projectX
  • -f sets the filename so you don’t overwrite your default key.
  • You’ll get two files:
  • ~/.ssh/id_rsa_projectX (private key)
  • ~/.ssh/id_rsa_projectX.pub (public key)

Add the public key to your GitHub/GitLab account for that specific project.

 

Step 2: Load the Key Using SSH Agent

Before cloning, start your SSH agent and add this new key:

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_projectX

This ensures the SSH agent knows which key to use when authenticating.
If your terminal or container forgets keys between sessions, you’ll need to re-run these lines.

 

Step 3: Configure SSH to Use the Right Key Automatically

Open your SSH config file:

nano ~/.ssh/config

Add a custom block like this:

Host github-projectX
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_projectX

Now, when you clone or push, use this alias instead of the regular GitHub domain:

git clone git@github-projectX:user/repo.git

Git sees github-projectX and automatically uses the key you assigned.

 

Step 4: Verify Your Connection

Test that your config works:

ssh -T git@github-projectX

If you see a greeting like “Hi username! You’ve successfully authenticated.” — you’re good to go.

 

Step 5: Optional — Manage Multiple Keys in One Session

If you switch between several repos often, you can list all loaded keys with:

ssh-add -l

And remove any that you don’t need:

ssh-add -d ~/.ssh/id_rsa_other

This keeps your agent clean and avoids confusion when you have many keys loaded.

 

Why Not Just Replace id_rsa?

Because it’s risky.
If you overwrite your default key, every repo, server, or CI pipeline linked to that key breaks.
Multiple SSH keys keep your work clean — one identity per project, one less headache later.

 

Quick Recap

  • Problem: You can’t clone a repo because your default SSH key is already tied to another account.
  • Solution: Create a new SSH key and configure it for that specific repo.
  • Command you’ll need:
eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa_projectX
  • Bonus: Use ~/.ssh/config to make Git remember which key goes with which host.

 

Final Thought

You don’t have to juggle logins or overwrite your trusted key just to clone a new repo.

A little SSH configuration lets you keep things organized — every project gets its own door key, and you decide which one to unlock.