Enable working with multiple github profiles on the command line using multiple SSH keys.
From the command line, generate an SSH key for each account to be accessed. Do this with care as you may already have an ssh key in use.
Generate ssh key pairs for accounts and add them to GitHub accounts. Do this with care; there may already be existing SSH keys in the local ~/.ssh/
directory. You will need one SSH key per GitHub identity. It is only necessary to generate keys for identities that do not yet exist.
ssh-keygen -t ed25519 -C "unique_identifier_work" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "unique_identifier_personal" -f ~/.ssh/id_ed25519_personal
Use a unique identifier that helps you identify this key in the future. The name is not strictly important.
Add github's public keys to ~/.ssh/known_hosts to allow frictionless sign in.
$ ssh-keyscan github.com >> ~/.ssh/known_hosts
It is possible to use SSH aliases for some versions of GIT. Some versions will ignore these aliases, but this is still good practice.
Edit (or create) the ~/.ssh/config
file and add an alias under the Host
parameter for one of the accounts. Match the appropriate identify file from the previous step with the correct profile.
# Personal GitHub account
Host github.com
HostName github.com
IdentitiesOnly yes
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519_personal
# School GitHub account
Host work_git
HostName github.com
IdentitiesOnly yes
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519_work
Test the aliases are configured properly with the correct key files and that the key files are registered at GitHub.
$ ssh -T work_git
Hi SlartyBartfast! You've successfully authenticated, but GitHub does not provide shell Access.
$ ssh -T github.com
Hi PlayerOne! You've successfully authenticated, but GitHub does not provide shell Access.
Any messages similar to this one indicate that there is an error in your configuration or you have not properly added the key to github.
$ ssh -T work_git
[email protected]: Permission denied (publickey).
The configuration should now allow you to clone repositories using any of the configured profiles.
Note that the personal identity is aliased to github.com
in the .ssh/config
file.
$ git clone [email protected]:PlayerOne/SpamHamRepo.git
Cloning into 'SpamHamRepo'
remote: Enumerating objects: 2332, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (68/68), done.
remote: Total 2332 (delta 41), reused 77 (delta 27), pack-reused 2236 (from 1)
Receiving objects: 100% (2332/2332), 2.20 MiB | 4.49 MiB/s, done.
Resolving deltas: 100% (637/637), done.
Note that the work identity is aliased to work_git
.
$ git clone git@work_git:SlartyBartfast/Magrathea.git
Cloning into 'Magrathea'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (4/4), 4.76 KiB | 2.38 MiB/s, done.
On macOS with HomeBrew's git version 2.39.5 (Apple Git-154)
, git does not appropriately use the ~/.ssh/config
aliases and will default to using the alias for github.com. This results in the error below:
$ git clone git@work_git:SlartyBartfast/Magrathea.git
Cloning into 'Magrathea'...
ssh: Could not resolve hostname work_git: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Workaround: Use the env variable GIT_SSH_COMMAND
. Note that github.com or the alias can be specified as the GIT_SSH_COMMAND
forces the appropriate key file to be used and overrides any other configuration options.
$ GIT_SSH_COMMAND="ssh -i ~/.ssh/id_ed25519_work" git clone [email protected]:SlartyBartfast/Magrathea.git
Cloning into 'Magrathea'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (4/4), 4.76 KiB | 2.38 MiB/s, done.
To ensure that the appropriate identity is used for creating commits and pulling/pushing to GitHub, it is important to use one of the following methods:
This option explicitly sets the identity to use for all actions in the repo. This only needs to be done once per cloned repo.
Change into each cloned repo and configure the it for the appropriate identity:
$ cd ~/src/Magrathea
$ git config user.email "[email protected]"
$ git config user.name "SlartyBartfast"
$ git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_work"
$ cd ~/src/SpamHamRepo
$ git config user.email "[email protected]"
$ git config user.name "PlayerOne"
$ git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_personal"
This option sets a global default identity for actions, and exceptions on a per-repo basis
This is the default identity.
$ git config --global user.name "PlayerOne"
$ git config --global user.email "[email protected]"
$ git config --global core.sshCommand "ssh -i ~.ssh/id_ed25519_personal"
This sets the identity for only the Magrathea
repo. Repeat for all other exceptions.
$ cd ~/src/Magrathea
$ git config user.email "[email protected]"
$ git config user.name "SlartyBartfast"
$ git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_work
If you are unable to clone a repo with the error below and you are certain that all of the keys and configuration files are in place, you may need to clear conflicting keys out of the ssh key agent.
$ git clone [email protected]:foo/spam.git
Cloning into 'spam'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
To clear the key agent use ssh-add -D
. Your keys will be added back as needed automatically if you have set AddKeysToAgent yes
in your .ssh/config
file.