Set Up SSH for Multiple Git Accounts: GitHub & GitLab Guide

How to set up separate SSH keys for GitHub and GitLab on the same machine — generate keys, configure ~/.ssh/config, and pin per-repo user.name and user.email so commits land on the right account.

Soman Bandesha Updated 3 min read
Set Up SSH for Multiple Git Accounts: GitHub & GitLab Guide

If you push to both a work GitHub and a personal GitLab from the same laptop, sooner or later you’ll commit with the wrong email and someone will notice. The fix is one SSH key per account, plus a small ~/.ssh/config that tells SSH which key to use for which host.

Here’s the setup I use day to day.

Step 1: Generate SSH Keys

First, generate a separate SSH key for each account.

GitHub

ssh-keygen -t rsa -b 4096 -C "your_github_email@example.com" -f ~/.ssh/id_rsa_github

GitLab

ssh-keygen -t rsa -b 4096 -C "your_gitlab_email@example.com" -f ~/.ssh/id_rsa_gitlab

These commands create two different SSH key pairs:

~/.ssh/id_rsa_github
~/.ssh/id_rsa_github.pub

~/.ssh/id_rsa_gitlab
~/.ssh/id_rsa_gitlab.pub

Step 2: Add SSH Keys to the SSH Agent

Start the SSH agent:

eval "$(ssh-agent -s)"

Then add both SSH keys:

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab

This allows your system to use the keys when connecting to GitHub or GitLab over SSH.

Step 3: Add SSH Keys to GitHub and GitLab

Next, copy each public key and add it to the correct platform.

Copy the GitHub SSH Key

cat ~/.ssh/id_rsa_github.pub

Add this key to your GitHub SSH keys settings.

Copy the GitLab SSH Key

cat ~/.ssh/id_rsa_gitlab.pub

Add this key to your GitLab SSH keys settings.

Only add the .pub files to GitHub and GitLab. Never share your private SSH keys.

Step 4: Configure the SSH Config File

Create or edit your SSH config file:

nvim ~/.ssh/config

Add the following configuration:

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

# GitLab
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa_gitlab

This tells SSH which key to use for each service.

Step 5: Clone Repositories Using SSH

When cloning repositories, use the SSH URL provided by GitHub or GitLab.

GitHub

git clone git@github.com:username/repository.git

GitLab

git clone git@gitlab.com:username/repository.git

SSH will automatically use the correct key based on the host.

Step 6: Configure Git User Information

After cloning a repository, set the Git username and email for that specific project.

GitHub Repository

cd path/to/github/repo
git config user.name "Your GitHub Name"
git config user.email "your_github_email@example.com"

GitLab Repository

cd path/to/gitlab/repo
git config user.name "Your GitLab Name"
git config user.email "your_gitlab_email@example.com"

This ensures commits are associated with the correct Git account.

Optional: Test Your SSH Connections

You can test your SSH connection to GitHub:

ssh -T git@github.com

And GitLab:

ssh -T git@gitlab.com

If everything is configured correctly, each service should recognize your account.

Summary

One SSH key per account, the host alias in ~/.ssh/config, and a per-repo user.name/user.email. SSH then picks the right key on its own, and you stop committing to the work account with your personal email.