NOTICE: This guide will help you set ssh keys for GitHub and GitLab. However, this is not going to change your commit
user.name
oruser.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:
- Open Git Bash.
- 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
- 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:
- Open Git Bash.
- 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.
- 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
- 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
- Open Git Bash.
- 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.
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- 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".
- Paste your key into the "Key" field.
- Click Add SSH key.
- If prompted, confirm your GitHub password.
- Open Git Bash.
- Paste the text below, substituting in your GitLab email address.
$ ssh-keygen -f ~/.ssh/id_ed25519_lab -t ed25519 -C "your_email@example.com"
- 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
- 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.
- 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
- Sign in to GitLab.
- In the top right corner, select your avatar.
- Select Settings.
- From the left sidebar, select SSH Keys.
- 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
orssh-rsa
, and may end with a comment. - In the Title text box, type a description, like Work Laptop or Home Workstation.
- 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.
- 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):
- Go to the .ssh directory
/c/Users/PC_USER_NAME/.ssh/
, click right mouse button and choose "Git Bash Here". - Create a file named "config" with the following command:
$ touch config
- Now open the config file with the command:
$ nano config
- 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.