Quickly toggle between two GitHub accounts using SSH keys.
You many need to remove all GitHub credentials from your MacOS Keychain, VSCode, GitHub Desktop, or other git apps before the SSH keys will work as expected.
You will need to create SSH keys for both your personal GitHub account and work account.
ssh-keygen -t rsa -C "YOUR_PERSONAL_ACCOUNT" -f ~/.ssh/id_rsa_personal
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_rsa_personal
Your public key has been saved in .ssh/id_rsa_personal.pub
You will see your fingerprint and a RSA image.
Repeat the above for the work account:
ssh-keygen -t rsa -C "YOUR_WORK_ACCOUNT" -f ~/.ssh/id_rsa_work
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_work
Use an editor of your choice to update the .ssh/config.
nano ~/.ssh/config
Update the config with your ssh values:
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
When cloning repositories you will need to use the SSH option. For repositories that have already been cloned you may need to reset a repository's origin to now use SSH. You may receive a 404 unauthorized response when attempting to push otherwise.
git remote set-url origin [email protected]:username/repository.git
Either hardcode your account details into the script, or setup environment variables using a .env file.
You can either add the toggle_github_account.sh file to a specific directory or to your PATH.
You can then call invoke the script:
./toggle_github_account.sh work
./toggle_github_account.sh personal
git config --global user.name
git config user.name