Multiple Github Accounts on One Windows System with SSH

As a developer, managing multiple GitHub accounts — such as one for work and another for personal projects — can be tricky. Switching between accounts


Here I am taking a Github Account & a Gitlab Account

Try to use the below commands with gitbash. If you are using this with the command prompt or Powershell, you may face some authentication issues.

  1. Generate SSH Keys for Both Accounts
  • Generate For GitHub Account
ssh-keygen -t rsa -b 4096 -C "gitlabMailid@gmail.com" -f ~/.ssh/id_rsa_gitlab
  • Generate For GitLab Account
ssh-keygen -t rsa -b 4096 -C "gitlabMailid@gmail.com" -f ~/.ssh/id_rsa_gitlab
  1. Add SSH Keys to the SSH Agent
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab

If You are facing any error while this process Try below command

eval “$(ssh-agent -s)”

Before Going to next process make sure in you .ssh folder two id_rsa files generated with given names, also along with that there should be two id_rsa_pub_yougivenname.pub files.

  1. Add SSH Key to Github/Gitlab
  • In github navigate to settings where you can see a section SSH & GPG Keys

  • In GitLab navigate to user settings where you can see a section SSH Keys

    Navigate to the above section and paste the key in your id_rsa.pub files to respective accounts.

Always copy the ssh key from the pub file and paste there. As our need is to authenticate user while git actions use key Type/Usage Type as Authentication

  1. Configure SSH Config File

    In the .ssh folder create a config folder as shown below

# GitHub account
Host github-personal.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github

# GitLab account
Host gitlab-office.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_gitlab
  1. Configure Git User Profile
git config --global user.name "work"
git config --global user.email "workmail@gmail.com"

If you are using an office laptop then try to configure the global git configure with your work mail.

For using personal git account go the project folder where you needed and initialize the the folder to git with git init.

git config user.name "personal"
git config user.email "personal@gmail.com"

Here, For the particular folder the configuration will be with your personal mail

  1. Clone Repositories Using Correct Config
#for office Account
git clone git@gitlab-office.com:username/repository.git

#for Personal Account
git clone git@github-personal.com:username/repository.git

Always clone with the SSH instead of HTTP