How to authenticate with GitHub using SSH

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet

Anytime you push code to GitHub, you must be authenticated so GitHub knows you are authorized to make changes. In this lesson we’ll learn how to authenticate with GitHub using SSH so we don’t have to enter our username and password each time we push code to GitHub.

[00:00] Now that we have Git configured locally on our machine, we have to authenticate our Git with GitHub so that when we push something to GitHub, GitHub knows who we are and whether or not we have access to push to where we're trying to push.

[00:13] We're going to configure this authentication with SSH. Open up your terminal, or if you're on a Windows machine, make sure that you're opening up Git Bash, and enter in the command ls -al .ssh in your home directory.

[00:26] If you see something like this, then that means you have not yet configured your ssh keys. If you see something that shows id_rsa or id_rsa.pub, or some pair like that, then you don't need to generate a new key and you're all set to go. You can skip this next step.

[00:43] Otherwise, you need to generate a new key. You can do this by executing ssh-keygen -t rsa -b 4096-C, and then you enter your email. I'll enter kent@doddsfamily.us. That will generate a new key pair. We can go with the default place to save this key pair and then we can enter a new pass phrase.

[01:05] If you wish to add an extra layer of security, you can add a pass phrase. We're going to omit this for now. We've now generated our ssh key. We can verify this by listing out the files in the ssh directory. We see that we have an id_rsa and an id_rsa.pub. That's what we need to authenticate with GitHub.

[01:21] Now, we need to add our ssh key to the ssh agent program. To do this, you'll type eval ssh-agent -s on a Mac. If you're on a Windows machine, make sure you type this in your Git Bash, ssh-agent -s. We need to add our rsa key to ssh, so we'll type in ssh-add and then point to our ssh key.

[01:46] Now, that we've configured ssh locally on our machine, we need to inform GitHub about our ssh key so when we push code, it can verify that key with GitHub. We're going to copy the public pair of our ssh key and give that to GitHub.

[02:01] On a Windows machine you can enter clip < and then point to your public rsa key. On a Mac, you use pb copy < and then point to your public rsa key. That's what I'll do.

[02:12] With your public pair of your ssh key on your clipboard, now you can go to GitHub. Go to your profile, edit profile, ssh keys, and add ssh key. Give this a title. I like to call it the name of my computer, so Hippotigris, and paste in your ssh key. Simply click add key, confirm your password, and then you have that key in your list of keys.

[02:40] We're not finished. We have to verify that GitHub and our local machine can communicate. We'll go back to our terminal and we'll enter ssh -T git@github.com. This will verify that we can connect to this host. We'll go ahead and type yes. It prints out "hi."

[03:02] If you get that message, then you have successfully connected your computer with GitHub. If we go back to our browser and hit refresh, you'll see the green icon here indicating that this key has been in use. We're ready to commit to GitHub.

[03:16] If you have trouble while doing this, GitHub has a document that you can look for to diagnose your issue. Just Google GitHub error permission denied public key. It will be the first result there, and you can work through some of the common troubleshooting process to figure out what your issue is.