How to use SSH Key instead of HTTPS with GitLab

Why?

SSH Key offer the ability to pull and push without the nessecity to type username and password everytime. So it is really a question of winning time and comfort.

Profile settings in GitLab

First of all go to your peronal settings in the upper right corner:

gitlabPersonalSettings

Then select SSH Key subcategory:

sshKeyCategory

You are now readdy to generate your keys.

Generate a public and private Key

The first step will be to generate a SSH Key for the version controlling (What is Git).

Platforms

Windows

I strongly recommend to Windows users to install Git BASH for Windows, so they will have the ability to use the same command as Unix/Linux based command interface. For that simply go to http://gitforwindows.org/ and download the latest release fitting your architecture.

After the installation simply run the Git BASH application, you will have something like that :

windowsGitBash

Mac

For mac users simply open a terminal and type git

Linux

For Linux simply get the git package sudo apt-get install git-all

Command to generate a new key

Before generating a new private and public key be sure that don’t already have one using the command :

cat ~/.ssh/id_rsa.pub

If you already have a key it will show something like this, jump below to copy your public key:

windowsGitBashWithRsaId

If you don’t have any key then write :

ssh-keygen -t rsa -C "your.email@example.com" -b 4096

This command will prompt you for a location and filename to store the key pair and for a password. When prompted for the location and filename, just press enter to use the default. If you use a different name, the key will not be used automatically.

generateSshKey

Copy-paste your public Key

Depending on your platform use the following command to copy your public key :

Windows

cat ~/.ssh/id_rsa.pub | clip

Mac

pbcopy < ~/.ssh/id_rsa.pub

Linux (requires xclip)

xclip -sel clip < ~/.ssh/id_rsa.pub

Back to Gitlab, you can now paste (CTRL-V) your key, give it a name like My Awesome PC, and click “Add key” to store it.

sshKeyCategoryAdd

Adding your SSH key to the ssh-agent

Start the ssh-agent in the background:

eval "$(ssh-agent -s)"

Add your SSH private key (the one without .pub at the end) 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_rsa in the command with the name of your private key file:

ssh-add ~/.ssh/id_rsa

List all the keys, you should see yours in within the ssh-agent:

ssh-add -l

Test your SSH connection

Before doing anything else try your connection to Gitlab with this command:

ssh -T git@gitlab.fabcloud.org

connectionTestSsh

You should recieve a Welcome message like that.

Retrieving an error

If you get any error at this stage don’t go further! You probably have an error like this Permission denied (publickey)., so please use the following link to debug https://help.github.com/articles/error-permission-denied-publickey/.

With all these steps you are now able to push without having to pu your username and password, enjoy!