How to configure Multiple GIT accounts in Computer


In this post, we will learn how to configure multiple github accounts on windows machine using SSH keys which is the most preferred way instead of HTTP authentication.

Here are the steps configure SSH keys in GITHUB. Let’s assume you have company and personal github account which you may need access to on your windows.

Configure GIT Hub account 1 with SSH Keys:

  1. Navigate to C:\Users\{userName}\.ssh. if folder is created, otherwise ignore it. ssh folder will be automatically created for you when you run ssh-keygen command

2. Open command prompt or gitbash which ever you comfortable.

3. Create ssh key file with with ssh-keygen command as shown in below. Leave empty value when you prompt for “Enter File in which to save”. It will create id_rsa file which is default name

 ssh-keygen -t rsa -b 4096 -C "jayaram@mycompany.com
"Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/jayaramp/.ssh/id_rsa):

4. Copy the content of id_rsa.pub file and paste it in your github ssh key settings. Logon to github.com with company account and navigate https://github.com/settings/profile

5. Access SSH details page: Profile ==> Setting ==> SSH and GPG Keys

6 Click on “New SSH Key” to add content of id_rsa.pub file

7. Add SSH Key by giving the Title as “MyCompanyKey” and paste the id_rsa.pub contents into the key text area.

8. Finally you configure SSH key and look like below

Now you can access company repositories with this account and by default id_rsa file will be loaded to communicate with github.com

Configure second GIT hub account with SSH key

As git clone, always refers id_rsa file then how to access personal github account. In this case, my personal user is jayaram509(jayaram@gmail.com) and create SSH key for this account

  1. Create SSH key for personal user “jayaram@gmail.com” as shown below:
$ ssh-keygen -t rsa -b 4096 -C "jayaram@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/jayaramp/.ssh/id_rsa): personal
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in personal
Your public key has been saved in personal.pub
 

2. In the Enter file name command, give name as personal and it creates SSH key with personal.pub follows as

3. Copy the SSH keys from personal.pub to jayaram509 github account as shown in below:

How to access personal GIT account now?

As shown in below, lets try to access the repo with git@github.com:jayaram509/helloworld-personal-repo.git

When you try to access the personal repository on your machine then by default it will load id_rsa instead of personal.pub

Create Config file in .ssh folder

  1. Navigate to again “C:\Users\jayaram\.ssh” folder
  2. create config file in .ssh folder as shown in below
Copy the below content to config file and save it.
Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa
Host github-personal-repo.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/personal

Make sure that config file should be created of type File not with of .txt extension, as shown in below

Now, if you try to access personal account repository with the host name as “github-personal-repo.com” which is Host in above config file

with that, new personal git clone URL is : git@ github-personal-repo.com:jayaram509/helloworld-personal-repo.git

D:\GITDemo>ssh -T github.com
Warning: Permanently added the RSA host key for IP address '13.234.176.102' to the list of known hosts.
jayaramp@github.com: Permission denied (publickey).

D:\GITDemo>ssh -T github-personal-repo.com
jayaramp@github.com: Permission denied (publickey).

D:\GITDemo>git clone git@github-personal-repo.com:jayaram509/helloworld-personal-repo.git
Cloning into 'helloworld-personal-repo'...
Enter passphrase for key '/c/Users/jayaramp/.ssh/personal':
warning: You appear to have cloned an empty repository.

Hope this helps. Now can you access multiple GITHUB accounts on Windows

Leave a Reply

Your email address will not be published. Required fields are marked *