Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouyagas/15cd5b80818897a912e4c907defd73b4 to your computer and use it in GitHub Desktop.
Save bouyagas/15cd5b80818897a912e4c907defd73b4 to your computer and use it in GitHub Desktop.
Managing SSH keys for Github and Gitlab
# GITHUB
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_hub
# GITLAB
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_lab

Managing SSH keys for Github and Gitlab

NOTICE: This guide will help you set ssh keys for GitHub and GitLab. However, this is not going to change your commit user.name or user.email. If you need to change those for specific repositories, just run the following commands while in your repository:

git config user.name "Your Name Here"
git config user.email your@email.com

For more info, see this answer. Also, keep in mind this only changes the .git folder inside your repository which never gets added/committed/pushed/uploaded.

I recently had to manage two ssh keys (one for Github and one for Gitlab). I did some research to find the best solution. I am justing putting the pieces together here.

The first question you can ask yourself is can you have the same ssh key for both Github and Gitlab? The answer is yes but it is not advisable.

The best answer is that you should set one ssh key for Github and another one for Gitlab. The first thing to do is install Git if you haven't. Next, you should check for existing ssh-keys on your system:

  1. Open Git Bash.
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present:
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
  1. Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key:

  1. Open Git Bash.
  2. Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -f ~/.ssh/id_ed25519_hub -t ed25519 -C "your_email@example.com"

Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

$ ssh-keygen -f ~/.ssh/id_rsa_hub -t rsa -b 4096 -C "your_email@example.com"

This creates a new ssh key, using the provided email as a label.

> Generating public/private ed25519 key pair.
  1. Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_hub in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_hub
  1. Open Git Bash.
  2. Copy the SSH public key to your clipboard.

If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.

$ clip < ~/.ssh/id_ed25519_hub.pub
# Copies the contents of the id_ed25519_hub.pub file to your clipboard

Tip: If clip isn't working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  1. In the upper-right corner of any page, click your profile photo, then click Settings.
  2. In the user settings sidebar, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal Mac, you might call this key "Personal MacBook Air".
  5. Paste your key into the "Key" field.
  6. Click Add SSH key.
  7. If prompted, confirm your GitHub password.
  1. Open Git Bash.
  2. Paste the text below, substituting in your GitLab email address.
$ ssh-keygen -f ~/.ssh/id_ed25519_lab -t ed25519 -C "your_email@example.com"
  1. Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_lab in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_lab

To use SSH with GitLab, copy your public key to your GitLab account.

  1. Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:

Git Bash on Windows:

cat ~/.ssh/id_ed25519_lab.pub | clip
  1. Sign in to GitLab.
  2. In the top right corner, select your avatar.
  3. Select Settings.
  4. From the left sidebar, select SSH Keys.
  5. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-ed25519 or ssh-rsa, and may end with a comment.
  6. In the Title text box, type a description, like Work Laptop or Home Workstation.
  7. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only, and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys.
  8. Select Add key.

Generally, in Windows machine, the SSH config file stored in the following location: /c/Users/PC_USER_NAME/.ssh/

Just follow the steps in below (if you're using the Git Bash):

  1. Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here".
  2. Create a file named "config" with the following command:
$ touch config
  1. Now open the config file with the command:
$ nano config
  1. Now write the following lines inside the config file:

Let's assume you've created two files named id_ed25519_hub for Github and id_ed25519_lab for GitLab.

# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_hub

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_lab

How can I save the file?

F3 will let you save without exiting. Otherwise, Ctrl + X will prompt you if you've made changes. Press Y when it asks, and Enter to confirm the filename.

How can I quit the editor without saving the changes?

Ctrl + X, then N when it asks if you want to save.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment