SSH config for two GitHub accounts: work and personal
Use two or three GitHub accounts on one computer with separate SSH keys and host names.
GitHub allows one SSH key per account. To use two accounts on one computer, give each its own key and its own host alias in your SSH config, then clone with the alias. Fill in the names and key files below.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesHow this setup works
Step-by-step setup
- Create a key for each account: ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_personal -C "personal", then the same for ~/.ssh/id_ed25519_work.
- Add each public key (the file ending in .pub) to its own GitHub account under Settings, SSH and GPG keys.
- Save the config above in ~/.ssh/config. On Linux and macOS run chmod 600 ~/.ssh/config.
- Test each alias: ssh -T git@github-personal should greet the first account, and ssh -T git@github-work the second.
- Clone with the alias: git clone git@github-work:company/repo.git. In an existing repo use git remote set-url origin git@github-work:company/repo.git.
- Set the author for each repo: git config user.email "you@work.com".
What the important lines mean
- Host is the alias you type. It is only a name on your computer.
- HostName github.com is the real server.
- IdentityFile is the private key for this account.
- IdentitiesOnly yes stops SSH from trying every key it knows, which could pick the wrong account.
Common errors and how to fix them
- Permission denied (publickey)
- GitHub does not know the key. Check the public key is added to the right account and that the path in IdentityFile is correct. ssh -vT git@alias shows which key is tried.
- Pushes go to the wrong account
- The remote still uses git@github.com. Change it to use the alias with git remote set-url.
- Bad owner or permissions on the config file
- SSH needs the file private. Run chmod 600 ~/.ssh/config and make sure your user owns it.
- Commits show the wrong name
- SSH only decides who you log in as. The commit name and email come from git config, so set user.name and user.email in each repo.
- Could not resolve hostname
- You typed the alias wrongly in the clone address, or the config file is not at ~/.ssh/config.
Frequently asked questions
- Why can I not use the same key for two accounts?
- GitHub allows a given SSH key on only one account, so each account needs its own key.
- Can I use HTTPS instead?
- Yes. With HTTPS you sign in with a personal access token for each account, often through a credential manager. SSH aliases avoid the repeated prompts.
- Does this work with GitLab or Bitbucket?
- Yes. Change HostName to gitlab.com or bitbucket.org and add the key to that service.
Fix a problem with this setup
- Git push rejectedFix "! [rejected] main -> main (fetch first)" and "failed to push some refs": pull the remote changes, handle protected branches and big files.
More developer configs
- SSH config fileGive servers short names and choose the right key for each.
- .gitconfig setupSet your name, email, default branch and shortcuts once.
- .env file exampleCreate a .env file for your app: port, database address and a secret key.
- VS Code settings.jsonSet font, tab size, format on save and theme in your VS Code settings.
- .bashrc and .zshrc aliasesAdd handy shortcuts and settings to your terminal.
- .gitignore fileTick your languages and tools to get a .gitignore that keeps build files and secrets out of Git.