How to switch between GitHub accounts using SSH to access the repositories
Navaneeth Vijay
1. Set Up SSH Keys for Each Account
If you haven't already, generate separate SSH keys for each GitHub account:
# For personal accountssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com"# Save it as ~/.ssh/id_rsa_personal# For work accountssh-keygen -t rsa -b 4096 -C "your_work_email@example.com"# Save it as ~/.ssh/id_rsa_work
Add each key to the SSH agent:
# Load personal keyssh-add ~/.ssh/id_rsa_personal# Load work keyssh-add ~/.ssh/id_rsa_work
2. Configure SSH Config File
Set up an SSH config file to specify which key to use for each account. Edit (or create)
~/.ssh/config
# Personal accountHost github-personalHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_personal# Work accountHost github-workHostName github.comUser gitIdentityFile ~/.ssh/id_rsa_work
3. Clone Repositories Using the Correct Host
When you clone a repository, use the Host alias specified in your SSH config (github-personal or github-work):
# For personal accountgit clone git@github-personal:username/repo.git# For work accountgit clone git@github-work:username/repo.git
4. Switching Accounts for Existing Repositories
For an existing repository, update the remote URL to use the appropriate SSH alias:
git remote set-url origin git@github-personal:username/repo.git