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

Managing multiple Git accounts can be challenging, but by using SSH keys, you can easily switch between different accounts for GitHub and GitLab. Here's a step-by-step guide to help you set it up.

Git

Managing multiple Git accounts can be challenging, but by using SSH keys, you can easily switch between different accounts for GitHub and GitLab. Here's a step-by-step guide to help you set it up.

Step 1: Generate SSH Keys

First, generate SSH keys for each account if you haven’t done so already. Open your terminal and run the following commands:

  • For GitHub:

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

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

Step 2: Add SSH Keys to the SSH Agent

Start the SSH agent and add your new SSH keys:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab

Step 3: Add SSH Keys to GitHub and GitLab

Copy the SSH keys to your clipboard:


For GitHub:

cat ~/.ssh/id_rsa_github.pub

For GitLab:

cat ~/.ssh/id_rsa_gitlab.pub
Go to GitHub and GitLab and add the respective keys to your SSH keys settings.

Step 4: Configure SSH Config

Create or edit the SSH config file:

nvim ~/.ssh/config

Add the following configurations:

# 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

Step 5: Clone Repositories Using SSH

When cloning repositories, use the SSH URLs provided by GitHub and GitLab:

  • GitHub:
git clone git@github.com:username/repository.git
  • GitLab:
git clone git@gitlab.com:username/repository.git

Step 6: Configure Git User Information

Set the Git user information for each repository to match the account:

  • For GitHub repository:
cd path/to/github/repo
git config user.name "Your GitHub Name"
git config user.email "your_github_email@example.com"
  • For GitLab repository:
cd path/to/gitlab/repo
git config user.name "Your GitLab Name"
git config user.email "your_gitlab_email@example.com"

Summary

By following these steps, you can manage multiple SSH keys for GitHub and GitLab, ensuring that the correct key is used for each service. This setup allows you to seamlessly work with multiple Git accounts without any hassle.