I got a new laptop and had to go through the pain for setting up my entire dev environment from scratch. After hours of downloading and installing tools I was ready to go. I tried to pull one of my old repos from GitHub and I got the good ol' Permission denied (publickey). That really sucked, not gonna lie, completely forgot about GitHub's SSH keys. Which is why I decided to write this post today.

I remember the first day I ran into this problem, it took me forever to find a solution. I hope this would make it a little easier for someone out there to get up and running in no time.

STEP 1

We'd first start by setting up our own public/private key pair set. This can use either DSA or RSA. Which means basically any key we set up will work, most systems use ssh-keygen.

First, we'd want to enter the .ssh directory. Open the terminal and run this command:

cd ~/.ssh

Next, we need to generate our own personalised ssh key, by running this command:

ssh-keygen

Now that our key has been generated, we'll need to copy it.

Note: the text that shows up after the command has been run is not what we need.

To copy the key, we'll need to run a command that is specific to whatever OS (and in some cases terminal) is running on the system so pick the one that fits your need.

Windows (Cygwin/Git Bash):

cat id_rsa.pub | clip

Windows (PowerShell):

Get-Content id_rsa.pub | Set-Clipboard

OSX:

cat id_rsa.pub | pbcopy

Linux:

cat id_rsa.pub | xclip

STEP 2

Next, we head over to GitHub to add the key to our account. When we get there, we log in (if you aren't already logged in). Once logged we're logged in we'll see the profile icon in the top right corner of the page, click on it. It will open a menu like this: Github's profile menu Go ahead and select the Settings option.

In settings there's a side menu on the left, select SSH and GPG keys. GitHub settings page

Now we're in the SSH and GPG keys section, we can now add our new SSH key which we copied earlier. Top right on the page you'll see a button that says New SSH Key, click on it. We'll get a page like this: Add new SSH page

In the Title field we'll give the SSH key a good description so we can know which device it belongs too. I normal use the make and year of the device, e.g. MacBook Pro 2020.

And in the Key field/textarea, that's where we paste the key we copied. Once we've done this, we can now hit the Add SSH Key button.

Once that is done, we'll see the key added to our list of keys: Example of SSH key on GitHub

STEP 3

We now need to configure our .gitconfig. Open the terminal and run these commands.

To add our username to our git config we'll run:

git config --global user.name "example"

Replace "example" with your own username.

To add our email to our git config we'll run:

git config --global user.email example@example.com

Replace example@example.com with your own email.

Once this is done, restart the terminal and we should be good to go to.

I hope this was of help 😊.

PS: I know some of you use Personal Access Tokens, maybe I'll make a post for those too