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.
Dev Kraken
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.
First, generate SSH keys for each account if you haven’t done so already. Open your terminal and run the following commands:
ssh-keygen -t rsa -b 4096 -C "your_github_email@example.com" -f ~/.ssh/id_rsa_github
ssh-keygen -t rsa -b 4096 -C "your_gitlab_email@example.com" -f ~/.ssh/id_rsa_gitlab
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
Copy the SSH keys to your clipboard:
cat ~/.ssh/id_rsa_github.pub
cat ~/.ssh/id_rsa_gitlab.pub
Go to GitHub and GitLab and add the respective keys to your SSH keys settings.
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
When cloning repositories, use the SSH URLs provided by GitHub and GitLab:
git clone git@github.com:username/repository.git
git clone git@gitlab.com:username/repository.git
Set the Git user information for each repository to match the account:
cd path/to/github/repo
git config user.name "Your GitHub Name"
git config user.email "your_github_email@example.com"
cd path/to/gitlab/repo
git config user.name "Your GitLab Name"
git config user.email "your_gitlab_email@example.com"
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.