DEV Community

Recca Tsai
Recca Tsai

Posted on • Originally published at recca0120.github.io

Three Ways to Git Clone with a Different SSH Key

Originally published at recca0120.github.io

You have access to a GitHub repo, but the SSH key isn't the default ~/.ssh/id_rsa or ~/.ssh/id_ed25519. Running git clone directly gives you Repository not found. This post covers three ways to specify which SSH key to use, including the common pitfall with ~/.ssh/config Host aliases.

The Problem

I needed to clone a private repo:

git clone git@github.com:client-org/project.git
Enter fullscreen mode Exit fullscreen mode

The SSH key required for this repo is ~/.ssh/client_key, not the system default. Cloning without specifying it returns:

ERROR: Repository not found.
fatal: Could not read from remote repository.
Enter fullscreen mode Exit fullscreen mode

The error message is misleading — the repo exists. GitHub returned "not found" because the SSH key it received doesn't have access. GitHub intentionally hides whether a repo exists at all when the key has no permission.

I tried setting a Host alias in ~/.ssh/config first, but clone still failed with the same error. The two methods below are what actually worked.

Method 1: GIT_SSH_COMMAND (One-Off)

The fastest approach — prefix the command with an environment variable:

GIT_SSH_COMMAND='ssh -i ~/.ssh/client_key' git clone git@github.com:client-org/project.git
Enter fullscreen mode Exit fullscreen mode

GIT_SSH_COMMAND tells Git to use the SSH command you specify. -i ~/.ssh/client_key points to the private key. This only applies to that one command — subsequent push/pull won't use it automatically.

Method 2: git config core.sshCommand (Single Repo)

After cloning, set it in the repo:

cd project
git config core.sshCommand 'ssh -i ~/.ssh/client_key'
Enter fullscreen mode Exit fullscreen mode

This writes to .git/config and applies to all future git operations (pull, push, fetch) inside that repo.

Do both in one shot:

GIT_SSH_COMMAND='ssh -i ~/.ssh/client_key' git clone git@github.com:client-org/project.git
cd project
git config core.sshCommand 'ssh -i ~/.ssh/client_key'
Enter fullscreen mode Exit fullscreen mode

This is the simplest approach for a single repo.

Method 3: ~/.ssh/config Host Alias (With a Catch)

The textbook approach is to set a Host alias in ~/.ssh/config:

Host github-client
    HostName github.com
    User git
    IdentityFile ~/.ssh/client_key
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

Then clone with the alias:

git clone git@github-client:client-org/project.git
Enter fullscreen mode Exit fullscreen mode

The Catch: Existing Host github.com Overrides the Alias

If your ~/.ssh/config already has a Host github.com block (e.g., for your personal account), SSH may ignore the alias and use the wrong key — clone fails with the same error.

Verify the alias is working before cloning:

ssh -T git@github-client
Enter fullscreen mode Exit fullscreen mode

A successful response is Hi <username>! with the correct account. If the wrong account shows up, the alias isn't routing correctly.

Add url.insteadOf to Route an Entire Org

If you have multiple repos under the same org that all need the same key, use a URL rewrite rule:

git config --global url."git@github-client:client-org/".insteadOf "git@github.com:client-org/"
Enter fullscreen mode Exit fullscreen mode

After this, any git operation targeting client-org repos automatically rewrites the URL to use the github-client alias — no need to set core.sshCommand on each repo individually.

For a single repo, core.sshCommand from Method 2 is more direct and doesn't touch global config.

Comparison

Method Best For Scope
GIT_SSH_COMMAND One-time clone Single command
git config core.sshCommand Single repo, ongoing use Single repo
~/.ssh/config + url.insteadOf Entire org, multiple repos Global

References

Top comments (0)