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 account
ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com"
# Save it as ~/.ssh/id_rsa_personal
# For work account
ssh-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 key
ssh-add ~/.ssh/id_rsa_personal
# Load work key
ssh-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 account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# Work account
Host github-work
HostName github.com
User git
IdentityFile ~/.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 account
git clone git@github-personal:username/repo.git
# For work account
git 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