Doing a bit of internet surfing, ended up using SSH keys to work with more than one GitHub account via the terminal. This is a common scenario for developers who need to separate work and personal projects, or contractors who work with multiple organizations.
1. Where are my SSH keys?
A Mac (and Linux) has SSH files in a .ssh directory in your home directory (~/.ssh).
If there is no config file, just create one
touch ~/.ssh/config
The config file is where we can configure SSH to use different keys for different hosts. This eliminates the need to manually specify which key to use each time you interact with a repository.
Generally all your SSH keys are saved to the directory ~/.ssh by default, but you can organize them however makes sense for your workflow.
take a look and see what you have there already.
cd ~/.ssh
ls -la
2. Generate your SSH keys for your GitHub accounts.
ssh-keygen -t ed25519 -b 4096 -C "your_email@example.com"
Note: I’m using ed25519 here instead of rsa because it’s more secure and performs better. Ed25519 keys are smaller, faster, and more secure than RSA keys. If for some reason you need RSA (older systems), you can use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
When you are prompted, it will show you the default suggested filename like id_ed25519.
Important: Don’t use the default filename! Instead, give each key a descriptive name that matches the account it’s for.
What we want to do is to create a SSH private and public key pair for each of the accounts you want to use on your machine.
So in this case I want to setup work and personal which will generate both the private and public keys.
Example filenames:
~/.ssh/github-work(private key)~/.ssh/github-work.pub(public key)~/.ssh/github-personal(private key)~/.ssh/github-personal.pub(public key)
The .pub extension indicates the public keys that you’ll add to your GitHub account settings.
3. cd back to ~/.ssh and update the config file
If there is no config file, no problem, you can create it now but hopefully you already created it from step 1 above.
For the config file, setup the file as listed below. The SSH config uses specific syntax and indentation matters:
# Work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/github-work
IdentitiesOnly yes
AddKeysToAgent yes
UseKeychain yes
# Personal account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/github-personal
IdentitiesOnly yes
AddKeysToAgent yes
UseKeychain yes
Key configuration options explained:
Host: This is the alias you’ll use in your git commandsHostName: The actual hostname (always github.com for GitHub)User: Alwaysgitfor GitHub SSH connectionsIdentityFile: Path to your private key fileIdentitiesOnly yes: Only use the specified key, don’t try othersAddKeysToAgent yes: Automatically add the key to SSH agentUseKeychain yes: On macOS, store passphrases in the keychain
The naming convention for the Host is important - it creates unique aliases so SSH knows which key to use for which account.
4. Add your public keys to your GitHub accounts
Before you can use the SSH keys, you need to add the public keys to your respective GitHub accounts:
- Copy your public key content:
cat ~/.ssh/github-work.pub
# Copy the output
- Go to GitHub.com → Settings → SSH and GPG keys → New SSH key
- Paste the public key content and give it a descriptive title
- Repeat for each account/key pair
5. When you clone your repo use the SSH section and modify the host
For this you will see in the clone URL, you’ll need to modify it to use your custom host alias.
Standard GitHub clone URL:
git clone git@github.com:work/your-repo-name.git
Modified to use your work account:
git clone git@github.com-work:work/your-repo-name.git your-repo-name_work
For your personal account:
git clone git@github.com-personal:personal/your-repo-name.git your-repo-name_personal
The key difference is replacing github.com with your custom host alias (github.com-work or github.com-personal).
6. You might want to configure your git identity for each repo you clone
This is crucial for keeping your commits properly attributed to the right account. You have a few options:
Option 1: Set per repository (recommended):
Open up local git config using git config --local -e and add:
[user]
name = Your Work Name
email = work@myworkemail.com
Option 2: Use git config commands:
git config user.name "Your Work Name"
git config user.email "work@myworkemail.com"
Option 3: Global config with conditional includes (advanced):
You can set up your global git config to automatically use different identities based on directory structure. In your ~/.gitconfig:
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
Then create separate config files like ~/.gitconfig-work with the appropriate user settings.
7. Make sure the remote url is in the right format
This is important for existing repositories or if you need to change the remote URL later.
Check your current remote URL:
git remote -v
The URL should look like:
git@github.com-work:work/your-repo-name.git
If this isn’t correct, you can set it using this command:
git remote set-url origin git@github.com-work:work/your-repo-name.git
Or you can also set it via the local git config by editing .git/config:
[remote "origin"]
url = git@github.com-work:work/your-repo-name.git
fetch = +refs/heads/*:refs/remotes/origin/*
8. Test your setup
You can test that your SSH keys are working correctly:
ssh -T git@github.com-work
ssh -T git@github.com-personal
You should see a message like: “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”