Like many others, I too have two GitHub accounts, one for personal use and another for work. I was having trouble using both accounts on my mac for personal and office projects. I tried some tutorials and none worked out for me not because they were wrong but the instructions were not comprehensive. In this article I am putting it together all the necessary steps to make it work.
This solution works for macOS Monterey (Version 12.x.x) and later.
Follow the steps below:
Assuming you have two different accounts on GitHub:
firstname.lastname@example.org. First, generate two different ssh keys for both of your accounts (follow the detailed instructions for generating ssh keys)
ssh-keygen -t ed25519 -C email@example.com
ssh-keygen -t ed25519 -C firstname.lastname@example.org
This will create public/private key pairs for each of these accounts. They should be located in
~/.ssh directory. Assuming generated key files are following
~/.ssh/id_ed25519_personal # Private key for personal account
~/.ssh/id_ed25519_personal.pub # Public key for personal account
Once the keys are generated run this command
eval "$(ssh-agent -s)"
You need to modify
/.ssh/config file. If it is already there then good else create it by
~/.ssh/config file and modify it as follows
#GitHub for personal
Host github.com # Notice the host for personal
IdentityFile ~/.ssh/id_ed25519_personal # Private key for personal account
#Github for work
Host github.com-work # Notice the host for work
IdentityFile ~/.ssh/id_ed25519_work # Private key for work account
# UseKeychain yes # If you choose a password to generate keys
AddKeysToAgent yes # Load the keys from keychain into ssh-agent automatically
IdentitiesOnly yes # Tells the ssh-agent server to use the IdentityFiles specified above for each host
Save the file. If you choose to add a password to generate your ssh keys, then uncomment line no. 14 in the above config file.
Do not add any identity file in
Host *as suggested here. We are using same host (
github.com) for both account. This will make the server to always choose the key for the first added identity to the
IdentitiesOnly yeswill prevent the server to make the connection using the first available key and force it to use the respective keys placed under different hosts above.
ssh-add -D # Will delete all identities added previously from the ssh agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
--apple-use-keychain option will store keys to the keychain and it will make keys persistent boot to boot.
ssh-add -dis used to remove identities then each keys from keychain will be removed. (Read the man page for
ssh-addfor more detail).
Check the added keys with
ssh-add -l if they are added.
At this point if you reboot your system then keys will be loaded to ssh-agent automatically from the keychain!
Do not skip this step!
Add public keys to your account on GitHub. Github already has good documentation for it. For more information, see Adding a new SSH key to your GitHub account.
Test the connection for both accounts
ssh -T email@example.com
ssh -T firstname.lastname@example.org
If you followed everything to this point, this should return
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
Now is the tricky part.
If you want to use your
personal account then copy the remote repo link from GitHub and clone it as
git clone email@example.com:personal/project.git
or reset if you already have cloned
git remote set-url origin firstname.lastname@example.org:personal/project.git
if you want to use your
work account then copy the remote repo url from GitHub and modify the url by replacing the host
email@example.com by the host
git clone firstname.lastname@example.org:company/project.git
Same rule for setting remote
git remote set-url origin email@example.com:company/project.git
Create a repo on GitHub and then clone it as instructed above. Change the current working directory to the local repository. Configure the user email and name (you can do globally too but I prefer this way) for this repository.
If you cloned the repo for
work account then configure it as
git config --local user.email "firstname.lastname@example.org"
git config --local user.name "work"
Now you can push/pull to/from GitHub remote repository.