I maintain multiple GitHub accounts on the same machine:
eric-rosencontinuallylearning
The cleanest and most reliable way to work with multiple GitHub accounts on a single computer is to use separate SSH keys and map each one to a custom SSH host. This avoids constantly switching credentials or accidentally pushing to the wrong account.
The setup steps involve:
- Create an SSH key for each GitHub account
- Assign a hostname to each SSH key
- Configure Git remotes to use the correct host
- Set git config user name and email
Create an SSH key for each GitHub account
Each GitHub account should have its own SSH key pair (public + private). Follow GitHub’s official documentation for each GitHub account, making sure to give custom-names for each SSH key. You can name these hosts and key files however you like—just keep them consistent:
- Generate a new SSH key and add it to the SSH agent:
GitHub Docs – Generating a new SSH key - Add the public key to your GitHub account:
GitHub Docs – Adding a new SSH key
After this step, you should have multiple private and public keys on your machine, one per GitHub account. For example, when I look at the files in the ~/.ssh folder, ear_ed25519 and ear_ed25519.pub are the private and public keys associated with the eric-rosen GitHub account, whereas cl_ed25519 and cl_ed25519.pub are the private and public keys associated with the continuallylearning account:
eric@ericbot:~$ ls -lh ~/.ssh
total 52K
-rw------- 1 eric eric 432 Dec 27 17:25 cl_ed25519
-rw-r--r-- 1 eric eric 118 Dec 27 17:25 cl_ed25519.pub
-rw-rw-r-- 1 eric eric 250 Dec 27 17:31 config
-rw------- 1 eric eric 419 Dec 27 17:30 ear_ed25519
-rw-r--r-- 1 eric eric 109 Dec 27 17:30 ear_ed25519.pubThe GitHub instructions tell you to add the ssh keys to the SSH agent via ssh-add /PATH/TO/PRIVATE_KEY, make sure you’ve done this by checking that all the keys are loaded. For example:
eric@ericbot:~$ ssh-add -l
256 SHA256:rVtbltX9gB1gA+oJ1tdsftbdnXduNdwzedqDKeIMlNE eric.andrew.rosen@gmail.com (ED25519)
256 SHA256:s19ObVbe6dheJtIPeuhRKJceoi6kV3oJzpdU6Rv/GVk continuallylearninggithub@gmail.com (ED25519)Assign a hostname to each SSH key
Next, edit your SSH config file:
~/.ssh/configDefine a separate host entry for each GitHub account:
# GitHub account alias for eric-rosen
Host github-eric
HostName github.com
User git
IdentityFile ~/.ssh/ear_ed25519
IdentitiesOnly yes
# GitHub account alias for continuallylearning
Host github-cl
HostName github.com
User git
IdentityFile ~/.ssh/cl_ed25519
IdentitiesOnly yesHere:
IdentityFilepoints to the private SSH key for that accountHostis a custom alias you’ll use instead ofgithub.comIdentitiesOnly yestells SSH to use only the key(s) specified byIdentityFilefor this host, and ignore any other keys loaded in the SSH agent.
Verify SSH access for each account
Test that each SSH key is correctly associated with its GitHub account:
ssh -T git@github-eric
ssh -T git@github-clEach command should authenticate successfully and indicate which GitHub account you’re logged in as.
Configure Git remotes to use the correct host
To push and pull using the correct account, your repository must:
- Use SSH (not HTTPS)
- Reference the correct SSH host
Go into directory of the repository, and check your current remote:
git remote -vExample output:
origin git@github.com:eric-rosen/eric-rosen.github.io.git (fetch)
origin git@github.com:eric-rosen/eric-rosen.github.io.git (push)If the host or username is incorrect, update it using the following format:
git@{host-name}:{repo_username}/{repo_name}.gitFor example:
git remote set-url origin git@github-eric:eric-rosen/eric-rosen.github.io.gitSet git config user name and email
Highly relevant resource about where git looks for this information and in what order At this point, you can push commits to your repository correctly. However, if you’d like to make sure that the shown authors of the commits represents your account, run this command to see the author name and account to be used:
git config --show-origin --get user.name
git config --show-origin --get user.emailIf this matches the account you want, great! If not, it may be grabbing that info from a git config (it’ll print it out). Otherwise, you can set the name and email directly, for example:
# for eric-rosen
git config user.name "Eric Rosen"
git config user.email "eric.andrew.rosen@gmail.com"
# for continuallylearning
git config user.name "continuallylearning"
git config user.email "continuallylearninggithub@gmail.com"Final checklist
ssh -Tworks for each custom host.git remote -vshows the correct SSH host and username.- Pushes and pulls go to the intended GitHub account.
- Commits are being shown to be authored by desired account.
If both the SSH test and the remote configuration are correct, you’re all set.