How to use a non-default SSH key when working with GitHub
- Generate a new SSH key:
ssh-keygen -t ed25519 -C "your_email@example.com"
Specify a new filename, e.g. ~/.ssh/id_org.
Upload the public key to the SSH and GPG keys page.
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 ofgithub.com.HostName github.com: The real hostname to connect to, SSH will open a connection togithub.comwhen you use theghorgalias.User git: The username used for the SSH connection. GitHub expects thegituser 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 specifiedIdentityFile(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.
- Use
ghorgas the hostname wherever you would usegithub.com, e.g.:
git clone git@ghorg:user/repo.git
