Use Multiple Git Accounts in a Single System (Windows)

Lets say we have two accounts.

  • account01
  • account02

1. Open PowerShell or Git Bash and run:


                # GitHub (account01)
ssh-keygen -t ed25519 -C "account01-github" -f ~/.ssh/id_github_account01
# GitHub (account02)
ssh-keygen -t ed25519 -C "account02-github" -f ~/.ssh/id_github_account02
 
 
# GitLab (account01)
ssh-keygen -t ed25519 -C "account01-gitlab" -f ~/.ssh/id_gitlab_account01
# GitLab (account02)
ssh-keygen -t ed25519 -C  "account02-gitlab" -f ~/.ssh/id_gitlab_account02
            

3. Start SSH Agent

If you are using Windows OpenSSH (default in Windows 10/11), ensure the SSH agent is running. In PowerShell as Administrator:


                Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
            

(If you strictly use Git Bash, you might use eval "$(ssh-agent -s)" instead)

4. Create and modify: ~/.ssh/config:


                # In Git Bash:
touch ~/.ssh/config
notepad ~/.ssh/config
 
# Or in PowerShell:
New-Item -Path ~/.ssh/config -ItemType File -Force
notepad ~/.ssh/config
            

Add this configuration:


                # GitHub (account01)
Host github.com-account01
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_account01
    IdentitiesOnly yes
 
# GitHub (account02)
Host github.com-account02
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_account02
    IdentitiesOnly yes
 
# GitLab (account01)
Host gitlab.com-account01
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_gitlab_account01
    IdentitiesOnly yes
 
# GitLab (account02)
Host gitlab.com-account02
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_gitlab_account02
    IdentitiesOnly yes
            

5. Add keys to the SSH Agent:


                # GitHub (account01)
ssh-add ~/.ssh/id_github_account01
# GitHub (account02)
ssh-add ~/.ssh/id_github_account02
 
# GitLab (account01)
ssh-add ~/.ssh/id_gitlab_account01
# GitLab (account02)
ssh-add ~/.ssh/id_gitlab_account02
 
# Verify all the keys are added 
ssh-add -l
            

6. Copy public keys (one at a time):


                # Copy GitHub key, then paste in GitHub Settings > SSH Keys
 
# GitHub (account01)
clip < ~/.ssh/id_github_account01.pub
# GitHub (account02)
clip < ~/.ssh/id_github_account02.pub
 
 
# Copy GitLab key, then paste in GitLab Settings > SSH Keys  
 
# GitLab (account01)
clip < ~/.ssh/id_gitlab_account01.pub
# GitLab (account02)
clip < ~/.ssh/id_gitlab_account02.pub
            

7. Test the connection:


                # GitHub (account01)
ssh -T git@github.com-account01
# GitHub (account02)
ssh -T git@github.com-account02
 
# GitLab (account01)
ssh -T git@gitlab.com-account01
# GitLab (account02)
ssh -T git@gitlab.com-account02
            

8. Use the SSH for account01


                # for new project
git clone git@github.com-account01:git-username/repository.git
 
# for existing project 
git remote set-url origin git@github.com-account01:git-username/repository.git