GitHub Account Switcher Guide
Summary Overview
Step-by-step process for managing multiple SSH keys, configs, and git identities across personal, client, and work accounts.
GitHub Account Switcher - Complete Guide
Table of Contents
- Overview
- Prerequisites
- Step-by-Step Implementation
- Understanding SSH Keys
- SSH Config Management
- Git Configuration
- Troubleshooting
- Best Practices
- Quick Reference
Overview
This guide teaches you how to switch between different GitHub accounts when using git push. This is essential when you have multiple GitHub accounts (personal, work, client projects) and need to push to different repositories using different accounts.
Why This is Needed
- Multiple GitHub Accounts: Personal, work, client accounts
- Repository Access: Different accounts have access to different repositories
- Security: Each account has its own SSH keys and permissions
- Attribution: Commits should be attributed to the correct account
Prerequisites
Before starting, ensure you have:
- Multiple GitHub accounts
- SSH access to your system
- Basic understanding of Git and SSH
- Terminal/command line access
Step-by-Step Implementation
Step 1: Generate New SSH Key for Target Account
1.1 Navigate to SSH Directory
cd ~/.ssh
1.2 Generate New SSH Key
ssh-keygen -t ed25519 -C "your-target-email@example.com" -f ~/.ssh/id_ed25519_targetaccount
Explanation:
-t ed25519: Use ED25519 encryption (more secure and faster than RSA)-C "email": Comment with your target GitHub account email-f ~/.ssh/id_ed25519_targetaccount: Custom filename for the key
1.3 Example Command
ssh-keygen -t ed25519 -C "flordgoat@gmail.com" -f ~/.ssh/id_ed25519_flordgoat
Step 2: Add SSH Key to SSH Agent
2.1 Start SSH Agent (if not running)
eval "$(ssh-agent -s)"
2.2 Add Your New Key
ssh-add ~/.ssh/id_ed25519_targetaccount
2.3 Verify Key is Added
ssh-add -l
Step 3: Add SSH Key to GitHub Account
3.1 Copy Public Key
cat ~/.ssh/id_ed25519_targetaccount.pub
3.2 Add to GitHub
- Go to GitHub.com
- Log in to your target account
- Go to Settings → SSH and GPG keys
- Click "New SSH key"
- Title: Give it a descriptive name
- Key: Paste the public key content
- Click "Add SSH key"
Step 4: Configure SSH Config File
4.1 Create/Edit SSH Config
nano ~/.ssh/config
4.2 Add Configuration
# Default GitHub account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Target GitHub account
Host github-target
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_targetaccount
Step 5: Test SSH Connection
ssh -T git@github-target
Step 6: Configure Git Remote
git remote set-url origin git@github-target:username/repository.git
Step 7: Configure Git User Information
git config user.name "Your Target Username"
git config user.email "your-target-email@example.com"
Troubleshooting
Common Issues and Solutions
Issue 1: "Permission denied (publickey)"
Cause: SSH key not added to GitHub or wrong key being used Solution:
ssh-add ~/.ssh/id_ed25519_targetaccount
ssh -T git@github-target
Issue 2: "No GitHub account was found matching the commit author email"
Cause: Git email doesn't match GitHub account email Solution:
git config user.email "correct-email@example.com"
Best Practices
Security
- Use strong passphrases for SSH keys
- Never share private keys
- Use ED25519 keys when possible
- Regularly rotate keys
Organization
- Use descriptive key names
- Use descriptive SSH host aliases
- Document your setup
Quick Reference
Essential Commands
# Generate new SSH key
ssh-keygen -t ed25519 -C "email@example.com" -f ~/.ssh/id_ed25519_account
# Add key to SSH agent
ssh-add ~/.ssh/id_ed25519_account
# Test SSH connection
ssh -T git@github-account
# Update git remote
git remote set-url origin git@github-account:username/repo.git
# Configure git user
git config user.name "Username"
git config user.email "email@example.com"
Conclusion
You now have a complete understanding of how to switch between GitHub accounts when using git push. This setup allows you to work with multiple GitHub accounts seamlessly while maintaining proper commit attribution and repository access.