Setup and Manage Multiple git accounts in windows

How to setup multiple git accounts including AWS Codecommit with SSH key authorization on windows

Introduction

We may have multiple git accounts one for official and one personal or when we work on cloud like AWS you may have many repositories in different account so you have multiple username to setup. We can't keep on changing the settings every time. So what to we do

We can solve the problem by setting the alias name for every git accounts. What ? How can we do that here we go

Generate SSH Key

Open you gitbash terminal then navigate to required path and type the below command

ssh-keygen

then enter the key preferred key name and press enter for remaining options

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/boopathikumar/.ssh/id_rsa): boopathi_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in boopathi_rsa.
Your public key has been saved in boopathi_rsa.pub.
The key fingerprint is:
SHA256:7Nr/m0cI+tw+RWUR4JF0slTzGCg78BDw+YhW9nUTN2s boopathikumar@LAPTOP-B9TJ4GJ
The keys randomart image is:
+---[RSA 3072]----+
|      ....  .B*B+|
|       .o.. +.==B|
|        =+ o.o+E.|
|       = +=. .o. |
|      o S.oo o   |
|     . ..   . o  |
|        .o . o   |
|       o  o o..  |
|      . ...o=+   |
+----[SHA256]-----+

now you can see two keys created as below in the path you navigated in gitbash

boopathi_rsa
boopathi_rsa.pub

Adding SSH Key in github

In your github account go to settings page and click option SSH and GPG Keys option and press New SSH Key button, the give preferred title and copy paste the content from boopathi_rsa file into the text box and save as shown below

github add key

Once key is added you will see as below

github add key

Adding SSH Key in AWS Code Commit

Login to the AWS account and navigate to IAM service and open your IAM user. Under the Security Credentials tab you will find Upload SSH public key button click that and copy paste the content from boopathi_rsa file into the text box and save as shown below

AWS add key

Once key is added you will see as below and copy the SSH key ID for later use in below steps

github add key

Setup configuration file

Create a folder new folder in the name of .ssh inside your user folder by giving folder name as .ssh..

Now you need to create file in the name config with no extension inside .ssh folder. Finally you will have folder structure like below.

C:\Users\<your-username>\.ssh\config

the structure of configurations inside config file as below

  • Host - Preferred alias name for your reference
  • Hostname - actual git account hostname
  • User - Only for AWS code commit
  • PreferredAuthentications - to authorize with ssh key publickey
  • IdentityFile - path of you pem key
Host my.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile "C:/Users/boopathikumar/.ssh/boopathi_rsa"

Host office.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile "C:/Users/boopathikumar/.ssh/boopathi_rsa2"

Host my_codecommit
    Hostname git-codecommit.us-east-1.amazonaws.com (only access to repo in us-east-1)
    User  APKAVDUCNF4JYDGHGTNQ (copied SSH key ID)
    PreferredAuthentications publickey
    IdentityFile "C:/Users/CloudKinetics/.ssh/boopathi_rsa"

Host office_codecommit
    Hostname git-codecommit.*.amazonaws.com (to access all region)
    User  XXXXXXXXXXXXXXXXXXXX  
    PreferredAuthentications publickey
    IdentityFile "C:/Users/CloudKinetics/.ssh/boopathi_rsa"

now all the configurations are set to clone the repository

Clone the Repository

Now I am trying to clone the private repository from my personal github account for that get the clone SSH URL of the repository as show below

github clone

you will get a clone SSH URL of the repository like below

git clone git@github.com:boopathikumar018/sample_repo.git

In this I am changing the Domain name (github.com) to configured Alias name (my.github.com) now the you URL will be like

git clone git@my.github.com:boopathikumar018/sample_repo.git

now automatically repo got cloned using the credentials under the Alias name in config file

Now I am trying to clone the AWS code commit repository from my AWS account for that get the clone SSH URL of the repository as show below

AWS clone

you will get a clone SSH URL of the repository like below

git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/sample-repo

In this I am changing the Domain name (git-codecommit.us-east-1.amazonaws.com) to configured Alias name my_codecommit) now the you URL will be like

git clone ssh://my_codecommit/v1/repos/sample-repo

As same automatically repo got cloned using the credentials under the Alias name in config file

You can configure as many git account like this and use easily by changing to Alias name.