Using multiple SSH keys for multiple Github accounts

Ondrej Kvasnovsky
1 min readOct 7, 2023

--

Option 1: Use core.sshCommand Configuration

Requires you to run a command in each repository, probably easier than changing remote origin in all your repositories.

Open a terminal or command prompt and navigate to your Git repository’s directory:

cd /path/to/your/repository

Set the core.sshCommand configuration for the repository to use a specific SSH key, for example the id_rsa key:

git config core.sshCommand "ssh -i ~/.ssh/id_rsa"

Option 2: Change Remote URL

This option requires you to change remote origin URL to include an alias, that is then used to pick the proper SSH key.

Open a terminal or command prompt and navigate to your Git repository’s directory: cd /path/to/your/repository

Open or create your SSH configuration file for the specific alias (<alias>) and SSH key (<key>) you want to use for the repository:

sublime ~/.ssh/config

Add the following SSH configuration, replacing <alias>, <key>, <username>, and <repository> with your specific values:

Host <alias>
HostName github.com
User git
IdentityFile ~/.ssh/<key>

<alias>: Your desired SSH alias.
<key>: The name of your SSH key file (id_rsa or id_ed25519 or so).
<username>: Your GitHub username.
<repository>: The name of your GitHub repository.

Change remote in your Git repo

You have to change the remote URL to use your configured SSH alias:

git remote set-url origin git@<alias>:<username>/<repository>.git

Verify the remote URL change:

git remote -v
origin git@<alias>:<username>/<repository>.git (fetch)
origin git@<alias>:<username>/<repository>.git (push)

Test the push:

git push origin <branch-name>

Related

--

--