baj.dev

Using different SSH keys for different GitHub repos

This can be configured using the SSH config file (/.ssh/config), by creating an alias for the hostname github.com and specifying the SSH-key to use:

Host github-one                               # alias
    HostName github.com                       # real hostname that will be used
    IdentityFile ~/.ssh/id_ed25519_github_one # The SSH key that will be used for this host

Host github.com                               # if no HostName is specified for this host, then Host is the real hostname
    IdentityFile ~/.ssh/id_ed25519            # The SSH key that will be used for this host

See https://superuser.com/a/1207305 for an explaination of Host vs HostName.

This says:

Now, all that remains to use the key for github-one is to use github-one as the hostname in the remote instead of github.com.

To update the git remote to use github-one in your repo, use this command: (assuming origin as the remote name)

git remote set-url origin git@github-one:<username>/<repo>.git