DEV Community

raven
raven

Posted on

Managing multiple GitHub account using git in windows

Wanna use another GitHub account in my windows pc but wait I already got my personal account added in git.
No problem here you can see how you can manage multiple accounts without any hassle:

Note: This DIY is meant for windows users.

Contents

*Set up SSH Keys.
*Add the keys to your Github accounts:
*Create a configuration file to manage the separate keys
*Update stored identities
*Test PUSH
*Test PULL


Set up SSH Keys

So let's consider your two GitHub accounts are named githubPersonal and githubWork, respectively.

Create two SSH keys, saving each to a separate file:
Execute the below mentioned commands in gitbash

$ cd ~/.ssh

$ ssh-keygen -t rsa -C "your_email_id_associated_with_githubPersonal_account

save it as id_rsa_personal when prompted (you can rename personal)
$ Enter passphrase (empty for no passphrase): skip this by pressing enter
$ Enter same passphrase again: skip this by pressing enter

$ ssh-keygen -t rsa -C "your_email_id_associated_with_githubWork_account"

save it as id_rsa_work when prompted (you can rename personal)
$ Enter passphrase (empty for no passphrase): skip this by pressing enter
$ Enter same passphrase again: skip this by pressing enter

The above commands setup the following files:
id_rsa_personal
id_rsa_personal.pub
id_rsa_work
id_rsa_work.pub


Add the keys to your Github accounts:

Copy the key to your clipboard by opening the file in vs code:

$ code . id_rsa_personal.pub

Add the key to your account:
Go to your GitHub Account Settings
Click “SSH Keys” then “Add SSH key”
Paste your key into the “Key” field and add a relevant title
Click “Add key” then enter your GitHub password to confirm.

Repeat the process for your githubWork account.


Create a configuration file

Create a configuration file to manage the separate keys in ~/.ssh/ using
$ touch config

Edit the file using the text editor of your choice. I used vs code -
$ code . config

# githubPersonal
Host personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal

# githubWork
Host work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work 


Update stored identities

initialize the ssh-agent (use powershell)

Get-Service -Name ssh-agent | Set-Service -StartupType Manual

Clear currently stored identities:
$ ssh-add -D
Add new keys:

$ ssh-add id_rsa_personal

$ ssh-add id_rsa_work

Test to make sure new keys are stored:

$ ssh-add -l

Test to make sure Github recognizes the keys:

$ ssh -T personal

Hi githubPersonal! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T work

Hi githubWork! You've successfully authenticated, but GitHub does not provide shell access.


Test PUSH

On Github, create a new repo in your personal account, githubPersonal, called test-personal.
Back on your local machine, create a test directory:
$ cd ~/documents
$ mkdir test-personal
$ cd test-personal
Add a blank “readme.md” file and PUSH to Github:
$ touch readme.md
$ git init
$ git add .
$ git commit -am "first commit"
$ git remote add origin git@personal:githubPersonal/test-personal.git
$ git push origin master
Notice how we’re using the custom account, git@personal, instead of git@github.com.

Repeat the process for your githubWork account.

Top comments (9)

Collapse
 
akhilbellam profile image
Akhil bellam

After I finished adding my personal account and start adding the work account
I get the following message
git@github.com: Permission denied (public key)

Do I need to switch between the accounts somehow? I think it's still trying to use the personal account for cloning the test-work repo

Also the ssh-agent has to be setup or started manually everytime from now?

Collapse
 
akhilbellam profile image
Akhil bellam

Found a solution to the issue:
freecodecamp.org/news/manage-multi...

Collapse
 
vtranwd profile image
vtranwd

Hi there! Newbie here. I'm running into an error that won't allow me to push onto my Github repo. My terminal did not stop me when I ran this:
$ git remote add origin git@personal:github.com/personal/test-personal.git

So when I ran this:
git push origin master
I get this error:
fatal: protocol 'git@personal:https' is not supported

Can anybody show me how to fix this? I haven't been able to find anything on google that could help. Should I just delete the repo and start over? My ssh has already been authenticated.

Collapse
 
phuocding profile image
phuocding

You forget config your name and email, right?

Collapse
 
raven404 profile image
raven

well, I haven't used my credentials in the post as it's a generic one instead I have mentioned the place where you need to put your credentials.

Collapse
 
jtnotes profile image
jtnotes

Great, it works like a charm! Thanks so much!

Collapse
 
zubairniazi profile image
Zubair Niazi

After a lot of hassle, I finally got it working. Thanks, man! :)

Collapse
 
hat52 profile image
Hamza Ali

Error connecting to agent: No such file or directory
I am getting this error after running ssh-add -D

Collapse
 
manupatel007 profile image
Vishwas Patel

Future readers, just remember to run powershell as administrator to avoid conflicts.