This tutorial will help you to configure your GitLab SSH keys. Once configured, you should be able to clone repositories, push your changes and submit your pull requests to GitLab repositories via SSH.
Unless otherwise noted, all instructions are for the Window environment. If you are looking for instructions for Linux or MAC OS, please consult the official documentation instead.
If not already installed, install Git for Windows.
Open Git Bash and generate a new SSH key pair:
$ ssh-keygen -t rsa -b 4096 -C "your-email@example.com"Click through the default settings.
You should have successfully generated two keys under ~/.ssh/:
- id_rsa - the private key
- id_rsa.pub - the public key
Copy the contents of the id_rsa.pub into your GitLab console and click Add key.
Open Git Bash and generate a new SSH key pair:
$ ssh-keygen -t rsa -b 4096 -C "your-email@example.com"Choose a custom name for your key: ~/.ssh/gitlab_rsa_example.
Leave the pass-phrase empty.
You should have successfully generated two keys under ~/.ssh/:
- gitlab_rsa_example - the private key
- gitlab_rsa_example.pub - the public key
Copy the contents of the gitlab_rsa_example.pub into your GitLab console and click Add key.
Since your are using a custom key name, gitlab_rsa_example instead of the default id_rsa, you must configure the SSH client to find it:
$ eval $(ssh-agent -s)
$ ssh-add ~/.ssh/gitlab_rsa_exampleTo retain these settings, you'll need to save them to a configuration file.
Create a configuration file ~./ssh/config and paste the following settings:
# GitLab | Example
Host YOUR-USERNAME.gitlab.com
Host gitlab.com
Preferredauthentications publickey
IdentityFile ~/.ssh/gitlab_rsa_exampleIf something went wrong in the configuration steps above, or if an identity becomes obsolete, you can remove it altogether.
List the stored ssh keys:
$ ssh-keygen -lGenerate a public key from the private key (do this even if you already have the pub):
$ ssh-keygen -y -f ~/.ssh/gitlab_rsa_example > ~/.ssh/gitlab_rsa_example.pubAnd then call ssh-add to remove the identity:
$ ssh-add -d ~/.ssh/gitlab_rsa_example.pubThe agent may refuse to remove the identity:
Could not remove identity "~/.ssh/gitlab_rsa_example.pub": agent refused operation
Try reverting our changes from step Storing the SSH Identity, by deleting the relevant Host entry, and then retry removing the identity.
Run the following command in your terminal:
$ ssh -T git@YOUR-USERNAME.gitlab.comThe first time you connect, you should see a warning:
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts.
Answer yes to add GitLab.com to the list of trusted hosts in a ./ssh/known_hosts file.
Run the above command again. You should now see the following message:
Welcome to GitLab, @username!
$ git remote add origin git@YOUR-USERNAME.gitlab.com/repository-name.gitCongrats! You are now ready to pull and push code to GitLab repositories.
In your config file example, I think it should say
HostName gitlab.cominstead ofHost gitlab.com. Cheers, Mike