Tag Archives: gitlab

Generate an SSH Key Pair within Git Bash

28 Jan 2016

If you want to be able to interact with your Git account without using passwords then you need to create an SSH key pair. You will then share the public key with GitHub to indicate to your repo that your computer is trusted. The first thing to do is to create the keys.

There are a couple of ways to do this on Windows, the easiest IMO is to use Git Bash.

Launch Git Bash and then enter the following command:

$ ssh-keygen –t rsa –b 4096 –C ‘your-email@somewhere.com’

You will be prompted for the file name and location but the default will be shown to you, illustrated via the following example.

$ ssh-keygen -t rsa -b 4096 -C ‘test@test.com’
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Rick/.ssh/id_rsa):

Hit “Enter” to accept the default.

Next you will be asked for a password. You probably don’t want a password so that you can push to your Git repo without being asked to provide one. In that case, just hit “enter” and move on.

Below is an example showing the password step:

$ ssh-keygen -t rsa -b 4096 -C ‘test@test.com’
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Rick/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):

Next you will be asked to confirm the password – once you’ve confirmed it you will see a message indicating that both the private and public key were created:

Your identification has been saved in /c/Users/Rick/.ssh/id_rsa.
Your public key has been saved in /c/Users/Rick/.ssh/id_rsa.pub.
The key fingerprint is:
a1:51:c2:b0:f2:9h:4c:1c:eb:a6:71:01:30:b3:db:82 test@test.com

Two key files have been created. Next you probably need to copy the public key to your clipboard. Enter the following command to view your Public Key:

$ cat ~/.ssh/id_rsa.pub.

Your key will appear within the Git Bash console – copy the key text starting with “ssh-rsa” and ending with your entire email address.

In Windows you won’t be able to simply highlight the text itself at first. Hit the “CTRL + a” key combination to select everything. This enables the ability to do a normal “click drag” text selection. Select your Public Key and hit “CTRL + c” to copy it.

From here…. you likely want to use it with your GitHub account.

Or, if you are using GitLab then follow these steps:

  1. Log into GitLab and navigate to the project.
  2. In the upper right-hand corner select the options icons (cog)
  3. Click the “SSH Keys” button in the left column
  4. Click the green “Add SSH Key” button
  5. Enter a Title
  6. Paste the public key into the key textarea
  7. Click “Add key”

If you need to see all of your public keys you can do the following:

$ ls ~/.ssh/*.pub

Add SSH Key to GitLab on OS X

15 Jan 2016

Before you are able to clone a repo down from GitLab you’ll need to first generate a pair of SSH keys – a private key and a public key. These keys are used to authenticate your session in GitLab. The private key should never be shared and never be part of your repo. The public key is what will be shared with GitLab.

Create the key

On your mac start the terminal and type the following command:

$ ssh-keygen -t rsa -C "$your_email@somewhere.com"

You will be asked where to place the resulting key file. You should follow the example path that is presented to you since that path is where Git (actually, SSH on behalf of Git) will look for the key to see if it matches the public key that you will paste into your GitLab project (that’s a very simple and somewhat inaccurate description of what really happens but its close enough for our purposes). To accept the default path just hit Enter on your keyboard.

You will be asked for a password – you can leave it blank or supply a password at your discretion though the best practice is to enter a password especially if multiple people have access to your dev machine. Otherwise if your machine is secure you can leave out the password and thus enjoy transactions that don’t prompt for passwords.

View the Public Key

Enter this command into the terminal to view the public key you’ve just created:

$ cat ~/.ssh/id_rsa.pub

Your key will appear – copy the key text starting from ssh-rsa all the way to your username/host.

Add the Key to Your GitLab Profile

Log into GitLab and locate your profile settings (which is the cog wheel in the upper right row of icons). Then click “Add SSH Key”. Here you will be presented with a text area within which you will paste the public key you generated. Paste it in, add a description, then click “Create”.

Clone the Repo

You’re ready to go, again in GitLab, locate the desired project and note the project path. Copy it.

Next, in terminal, clone your repo by typing:

$ git clone paste_your_repo_path_here desired_folder_here

If you created the key with a password you will be prompted at this point to enter it.

The repo will be cloned to your machine.