How to use a non-default SSH key when working with GitHub

  1. Generate a new SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"

Specify a new filename, e.g. ~/.ssh/id_org.

  1. Upload the public key to the SSH and GPG keys page.

  2. Add a new section to ~/.ssh/config:

Host ghorg
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_org
    IdentitiesOnly yes

Explanation of each line in the SSH config:

  • Host ghorg: A short alias you choose for this host entry. Use this alias in Git remotes (for example, git@ghorg:...) instead of github.com.
  • HostName github.com: The real hostname to connect to, SSH will open a connection to github.com when you use the ghorg alias.
  • User git: The username used for the SSH connection. GitHub expects the git user for SSH Git operations.
  • IdentityFile ~/.ssh/id_org: The path to the private key file to use for this host. Replace this with the filename you created for the organization’s private key.
  • IdentitiesOnly yes: Instructs SSH to use only the specified IdentityFile (and not try other keys from your agent or default locations). This prevents the server from rejecting the connection because the wrong key was offered first.
  1. Use ghorg as the hostname wherever you would use github.com, e.g.:
git clone git@ghorg:user/repo.git