Managing Multiple GitHub Accounts with SSH Keys
Switch Between Personal and Work Accounts on a Single Machine
Managing multiple GitHub accounts on a single machine can be a challenging task. However, with the right configuration, you can seamlessly switch between your personal and work accounts without hassle. This guide will walk you through the process of setting up SSH keys for multiple GitHub accounts.
Note : For Window Use Git Bash
Step 1: Generate SSH Keys for Both Accounts
First, you need to generate SSH keys for your personal and work accounts. Open your terminal and use the ssh-keygen
command.
For your personal account:
ssh-keygen -t rsa -b 4096 -C "lsehotr###@gmail.co"
# When prompted, save the key as: /home/lovely/.ssh/id_rsa_personal(any name)
For your work account:
ssh-keygen -t rsa -b 4096 -C "l.sehotra##@work.com"
# When prompted, save the key as: /home/lovely/.ssh/id_rsa_work
Example
Step 2: List the Keys
Verify that the keys have been created successfully by listing the contents of the .ssh
directory:
ls ~/.ssh
Step 3: Copy the Public Keys
You need to add your public keys to GitHub. Display the contents of each public key file with the following commands:
For your personal account:
cat ~/.ssh/id_rsa_personal.pub
For your work account:
For your work account:cat ~/.ssh/id_rsa_work.pub
Step 4: Add the Keys to GitHub
Log in to GitHub.
Navigate to Settings > SSH and GPG keys.
Click New SSH key and add the appropriate public key for each account.
Step 5: Configure SSH
Next, you need to configure SSH to use the correct key for each account. Create and edit the SSH config file:
touch ~/.ssh/config
nano ~/.ssh/config
Add the following configuration. Set Host Name and Identity File :
Host github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
Step 6: Configure Git
You can also configure Git to use different user information for each account. Create and edit the Git configuration file:
nano ~/.gitconfig
Add your details:
[user]
name = Your Name
email = lsehotra###@gmail.com
[includesIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Create a Directory for Work then cd work and run :
nano .gitconfig
Add Detail
[user]
name = Your Work Name
email = l.sehotra##@work.com
Step 7: Manage SSH Keys
First, remove all keys:
ssh-add -D
Then, add your personal key:
ssh-add ~/.ssh/id_rsa_personal
Same for work account
ssh-add ~/.ssh/id_rsa_work
Step 8: Verify the SSH Connection
Verify that your SSH configuration is correct by testing the connection:
ssh -T github.com
ssh -T github.com-work
Example
Step 9: Clone Repositories
You can now clone repositories using the host name corresponds to account as specified in the SSH config file.
For your personal account:
git clone git@github.com:LovelySehotra/Chat-App-Ui.git
This command uses the default host configuration (github.com
), which corresponds to your personal account as specified in the SSH config file.
For your work account:
git clone git@github.com-work:Viladhan/backend.git
In this command, the host github.com
-work
corresponds to the work account as specified in the SSH config file.