GitHub Setup with SSH for Multiple Accounts
SSH (secure shell)
SSH means Secure Shell — it’s a protocol that allows two computers to talk securely.
For GitHub:
- SSH allows your computer to prove your identity to GitHub
- Without typing your username or password every time
- Using keys (a key pair: public + private)
SSH Keys: Think of Them Like a Lock & Key
SSH uses two keys:
Private Key (stored on your computer)
- Stays in
~/.ssh/id_rsaor similar - You never share this
- Acts like your personal secret key
Public Key (added to GitHub)
- Looks like
ssh-rsa AAAAB3.... - You upload this to GitHub
- Safe to share — it’s just a lock
When you “push” your code:
GitHub checks if your private key matches the public key saved on GitHub.
If yes → GitHub says “OK that is really you”
To Manage Multiple GitHub Account in Vs Code. we can configure SSH keys and use a different Git configuration for each account
Step 1: Generate SSH Keys for Both Accounts
-
Open a terminal in VS Code.
-
Generate SSH key for your personal account:
ssh-keygen -t rsa -b 4096 -C "personalAccount@gmail.com"- Save the key as
~/.ssh/id_rsa_personal.
- Save the key as
-
Generate SSH key for your office account:
ssh-keygen -t rsa -b 4096 -C "sOffice@work.com"- Save the key as
~/.ssh/id_rsa_office.
- Save the key as
Step 2: Add SSH Keys to the SSH Agent
-
Start the SSH agent:
eval "$(ssh-agent -s)" -
Add your personal key:
ssh-add ~/.ssh/id_rsa_personal -
Add your office key:
ssh-add ~/.ssh/id_rsa_officeStep 3: Add SSH Keys to GitHub
-
Copy the SSH keys to the clipboard:
- For personal:
cat ~/.ssh/id_rsa_personal.pub - For office:
cat ~/.ssh/id_rsa_office.pub
- For personal:
-
Add the SSH keys to your GitHub accounts:
- Go to GitHub Settings > SSH and GPG keys.
- Add a new SSH key and paste the content from the respective
id_rsa_personal.pubandid_rsa_office.pub.
Step 4: Configure SSH Config File
-
Edit your SSH config file:
nano ~/.ssh/config -
Add the following configuration:
# Personal GitHub account Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_personal # Office GitHub account Host github.com-office HostName github.com User git IdentityFile ~/.ssh/id_rsa_office
Step 5: Clone Repositories Using the Correct SSH Config
-
For personal projects:
git clone git@github.com-personal:username/repository.git -
For office projects:
git clone git@github.com-office:username/repository.git
Step 6: Configure Git User for Each Repository
-
Navigate to your project directory.
-
Set the user for personal projects:
git config user.name "Your Name" git config user.email "personalAccountgmail.com" -
Set the user for office projects:
git config user.name "Your Name" git config user.email "offiec@work.com"
Exception
- If you have configured Git globally, you may encounter issues where commits or pushes use the globally set account name, even though that account doesn't have permission to access the repository.
git config --global user.name "Your Name"
git config --global user.email "offiec@work.com"- To Fix This we have remove git config globally
git config --global --unset user.name git config --global --unset user.email