Manage GitHub, and Gitlab accounts on single machine with SSH keys on Mac
For this example, I will use the same email to produce two different SSH keys: 1 for Github, and 1 for Gitlab. The logic also extends to different emails, and multiple Github, and Gitlab accounts.
Step 1. Generate new SSH keys
Check if you have any existing SSH keysls -al ~/.ssh
on your machine. You can reuse the existing key pair if it is available, but I suggest that you don’t.
To create private and public SSH pairs for your personal, and work accounts, run:
ssh-keygen -t rsa -C "personal@mail.com" -f ~/.ssh/id_rsa_gitlab
ssh-keygen -t rsa -C "personal@mail.com" -f ~/.ssh/id_rsa_github
These will produce four different files namely:
~/.ssh/id_rsa_github
~/.ssh/id_rsa_github.pub
~/.ssh/id_rsa_gitlab
~/.ssh/id_rsa_gitlab.pub
Step 2. Add SSH keys to Github, and Gitlab
Github
Copy corresponding public key of your Gitlab accountpbcopy < ~/.ssh/id_rsa_github.pub
Logon to your Github account. Then go to Settings > SSH and GPG Keys > New SSH key
Paste the public key on the Key
, and edit the Title
part as you please.
Gitlab
Copy corresponding public key of your Gitlab accountpbcopy < ~/.ssh/id_rsa_gitlab.pub
Logon to your Gitlab account. Then go to Settings > SSH Keys
Paste the public key on the Key
, and edit the Title
part as you please.
Step 3: Register SSH Keys with the ssh-agent
Register all SSH keys on your local machine using the ssh-agent
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
Step 4: Editing Config File
Create a configuration file that will add the different SSH keys for all online repositories and emails that we created earlier
touch ~/.ssh/config // Creates config file if it does not exist
vi ~/.ssh/config . // Edit config file
The config file should look like this
# This is a comment# Personal github account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github# Personal gitlab account
Host gitlab.com
HostName gitlab.com
User bgit
IdentityFile ~/.ssh/id_rsa_gitlab
After following the above steps, you should be able clone, and edit both your GitHub, and GitLab repositories on your local machine. Thanks!