Managing multiple GitHub accounts on the same computer can be challenging, but it’s essential for developers working with different clients or projects. This comprehensive guide shows you how to set up and manage multiple GitHub accounts using SSH keys on macOS, with practical examples and best practices.
Prerequisites
Before you begin, ensure you have:
- macOS (this guide is for macOS, but YMMV on other operating systems)
- Git installed
- SSH installed
- Access to your GitHub accounts
- Terminal access
- Basic understanding of SSH and Git concepts
Setting Up Multiple GitHub Accounts
1. Generate SSH Keys
Use the following shell function to generate and configure SSH keys for each GitHub account:
ssh_key_gen() {
if [ $# == 0 ]; then
echo "Usage: ${FUNCNAME} [EMAIL] [KEY_NAME]"
echo "1. Generate a key"
echo "2. Copy the public key to the clipboard"
return
fi
email="$1"
key_name="$2"
rsa_key_name="id_rsa_${key_name}"
rsa_key_path="${HOME}/.ssh/${rsa_key_name}"
ssh-keygen \
-t rsa \
-C "${email}" \
-f "${rsa_key_path}"
cat "${rsa_key_path}.pub" | pbcopy
ssh-add "${rsa_key_path}"
ssh_config_entry=$(cat <<-END
Host github.com
HostName github.com
User git
IdentityFile "${rsa_key_path}"
END
)
ssh_config_path="${HOME}/.ssh/config"
if ! grep "${ssh_key_name}" "${ssh_config_path}"; then
echo "Key already exists in ${ssh_config_path}"
else
echo "${ssh_config_entry}" >> "${ssh_config_path}"
fi
}
2. Configure Your GitHub Account
- Run the function with your email and key name:
ssh_key_gen your.email@domain.com your_key_name
- Enter your passphrase when prompted
- Add a new SSH key to your GitHub account
- Test your configuration by cloning a repository
3. Verify Configuration
# Test SSH connection
ssh -T git@github.com
# Check loaded keys
ssh-add -L
# Verify SSH config
cat ~/.ssh/config
Managing Multiple SSH Keys
Switching Between Accounts
To switch between different GitHub accounts, use these commands:
# Remove all loaded SSH keys
ssh-add -D
# Load your primary key
ssh-add ~/.ssh/id_rsa
# When done, remove the primary key
ssh-add -D
# Load your secondary key
ssh-add ~/.ssh/your_key_name
Managing SSH Keys
Use these utility functions to manage your SSH keys:
# Delete an SSH key
ssh_key_delete() {
if [ $# == 0 ]; then
echo "Usage: ${FUNCNAME} [KEY_NAME]"
echo "1. Delete a key"
return
fi
key_name="$1"
rsa_key_name="id_rsa_${key_name}"
rsa_key_path="${HOME}/.ssh/${rsa_key_name}"
ssh-add -D "${rsa_key_name}" \
&& rm -f "${rsa_key_path}"* \
|| echo "Delete failed: ${rsa_key_path}"
}
# List all loaded SSH keys
ssh_key_list() {
ssh-add -L
}
Git Configuration
Per-Repository Settings
For repositories that need specific user information:
# Set local Git configuration
git config user.name "Your Name"
git config user.email "your.email@domain.com"
# Verify settings
git config --local -l
Global Settings
For default Git configuration:
# Set global Git configuration
git config --global user.name "Your Name"
git config --global user.email "your.email@domain.com"
# Verify settings
git config --global -l
Security Best Practices
SSH Key Management
- Use descriptive names for your SSH keys
- Keep your private keys secure
- Use strong passphrases
- Regularly rotate your SSH keys
- Never share private keys
Access Control
- Use different SSH keys for different accounts
- Implement key expiration policies
- Monitor key usage
- Revoke compromised keys immediately
Repository Security
- Use repository-specific Git configurations
- Implement branch protection rules
- Enable two-factor authentication
- Regular security audits
Troubleshooting
Common Issues
SSH Connection Problems
- Verify SSH configuration file (
~/.ssh/config
) - Check if SSH keys are loaded (
ssh-add -L
) - Test SSH connection (
ssh -T git@github.com
) - Ensure GitHub account has correct SSH key
- Verify SSH configuration file (
Git Configuration Issues
- Check local and global Git settings
- Verify repository-specific settings
- Ensure correct user information
- Check Git credentials
Permission Issues
- Verify SSH key permissions
- Check repository access rights
- Ensure correct account association
- Validate SSH agent status
Automation and Scripts
Key Management Script
Create a script to automate key management:
#!/bin/bash
# Load all keys for a specific account
load_github_keys() {
local account=$1
ssh-add -D
ssh-add ~/.ssh/id_rsa_${account}
}
# Switch between accounts
switch_github_account() {
local account=$1
load_github_keys ${account}
git config --global user.email "your.${account}@domain.com"
}