Hey people, so recently when I joined a new company, I faced a new problem regarding version control. I have been using my personal github from beginning, but there I had to use company's github to collaborate on the project and push new changes. But it was only for 1 project and I was working on 2 projects at the same time. For the other project I had to use my own github.
So, for the first few days I was juggling between 2 different accounts. I used to first login into company's account, work on the project, push new changes and then remove its credentials from Manage Github credentials and login my personal account again and then work on another project. So, as you can tell this was so overwhelming switching accounts again and again. So, I reasearched online and got the whole solution. I will provide the commands here for both linux and windows.
so follow these steps 1 by 1
1. Create ssh keys
first create ssh keys for both accounts using their emails.
Linux
Personal
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_personal
work
ssh-keygen -t rsa -b 4096 -C "work@example.com" -f ~/.ssh/id_rsa_work
Windows
ssh-keygen -t rsa -b 4096 -C "personalmail@gmail.com" -f %USERPROFILE%.ssh\id_rsa_personal
work
ssh-keygen -t rsa -b 4096 -C "workmail@gmail.com" -f %USERPROFILE%.ssh\id_rsa_work
if it says
Saving key "~/.ssh/id_rsa_personal" failed: No such file or directory
then create directory first
mkdir %USERPROFILE%.ssh
When prompted for passphrase, you can press Enter twice to leave it empty
Once done, your key pair will be created:
- Public key: C:\Users\PC-Name.ssh\id_rsa_personal.pub
- Public key: C:\Users\PC-Name.ssh\id_rsa_work.pub
2. Stast SSH agent and add these keys
After creating those above keys, you have to add those in ssh agent first so that it can recognize them later.
Linux
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_manager
Windows
For Powershell
Start-Service ssh-agent
Get-Service ssh-agent
for CMD
net start ssh-agent
If it says "The requested service has already been started", that’s okay, it means it’s running.
Now run
ssh-add $env:USERPROFILE.ssh\id_rsa_personal
ssh-add $env:USERPROFILE.ssh\id_rsa_work
You should see:
Identity added: C:\Users\PC-Name.ssh\id_rsa_personal
3. Create or edit the SSH config file
You will either have to create config file for ssh if it doesn't exist or you can edit one if its already there. That configuration will be used for pushing or pulling data later.
Linux:
first run
notepad ~/.ssh/config
then copy paste this below
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
Windows:
notepad $env:USERPROFILE.ssh\config
then copy paste this below
Host github.com-personal
*HostName github.com
User git
IdentityFile C:\Users\PC-Name.ssh\id_rsa_personal
Host github.com-work
HostName github.com
User git
IdentityFile C:\Users\PC-Name.ssh\id_rsa_work
Save and close.
4. Add SSH keys to both GitHub accounts
Now we have to add those ssh keys we created earlier to both of our accounts respectively so that our config we created in above step can authenticate and work properly
Linux:
cat ~/.ssh/id_rsa_personal.pub
cat ~/.ssh/id_rsa_work.pub
Windows:
cat $env:USERPROFILE.ssh\id_rsa_personal.pub
cat $env:USERPROFILE.ssh\id_rsa_work.pub
Go to GitHub → Settings → SSH and GPG keys → New SSH key, and add the right one to each account.
5: Update the repo to use the work account’s key
Inside the work’s repo folder, set the remote to use the work account’s host:
cd path/to/work-project
git remote set-url origin git@github.com-work:WorkUsername/repo-name.git
Notice how we used:
github.com-work
This matches the Host name you defined earlier in your SSH config:
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
For personal projects use
git remote set-url origin git@github.com-personal:YourUsername/repo-name.git
6: Use work’s name and email in that project
In the work’s project folder, set work's credentials locally (not global):
cd path/to/work-project
git config user.name "Work Account Name"
git config user.email "work-email@example.com"
7: Verify setup
verify all the settings and configuration we did above
git config user.name
git config user.email
Test the remote host:
ssh -T git@github.com-manager
ssh -T git@github.com-personal
Now:
- Your personal repos use → git@github.com-personal:...
- Your work repo uses → git@github.com-work:...
If you still have any questions, you can reach out to me on my email as sufyanliaquat58@gmail.com.
Thank you.
Top comments (0)