December 21, 2019
At some point, most likely in the very early days of your programming career, you'll commit some changes to git, push those changes, and will find yourself faced with the following error message:
The first time I ran into this I remember feeling completely and utterly confused. I knew I had access to the github account I was trying to push to but for some reason Git was telling me my permission had been denied??? I started googling the error trying to find the cause, and ultimately a solution, but what I discovered during this process was that I needed to to piece together several posts and docs to finally get everything up and running again. What's worse is, I then ran into the same issue again about a year later and, because it had been so long since I'd first resolved the problem, had completely forgotten why it had happened or what I needed to do. As with most things, it takes a few times of doing the same thing before it finally sticks.
Git and SSH Keys
Sometimes when you go to push to Git you may find yourself facing the above error, or perhaps you're being prompted for a password, and you may continue to be prompted for a password every time you push. The easiest way to stop that from happening is to add an SSH key to Github.
An SSH key basically provides more security than your standard password. SSH public key authentication provides a level of cryptographic strength that even the strongest passwords cannot offer. If you have a Github account, even if you aren't running into the error seen in the screenshot above, you have the option to add an SSH key, or multiple keys, to your account to avoid typing passwords when pushing or to simply up your security. If you do run into the above error, perhaps because you're trying to push to one repo from multiple machines or because you're an admin on another Github account and Github is trying to confirm you're really you, the steps you'll need to take to resolve it are simple.
The first thing you'll want to do is check if you already have an ssh key on your machine. You can do this by running
cd ~/.ssh && lsfrom your terminal.
cd ~:changes you into your home directory
/.ssh:will then move you into your .ssh directory if one already exists. If one doesn't exist you'l get an error.
&& ls:here you're chaining on a secondary command and listing the contents.
If you don't have an ssh directory or the directory is empty, you'll need to generate an ssh key. This can be done by running
ssh-keygen -o. You'll see 'Generating public/private rsa key pair', followed by the location where the file is being saved, and you'll be prompted to enter a passphrase which you can skip by hitting enter if you choose to.
Once that has completed, try running
cd ~/.ssh && lsagain. This time you should see something like:
cat id_ras.pubto view your ssh key in your terminal. Your key will start with 'ssh-rsa', be followed by by a ton of randomly generated uppercase and lowercase letters, numbers, and special characters, and finish with your email address. Viewing this will give you a pretty good sense of why an ssh public key easily trumps even the strongest of passwords.
Next you'll want to log into your Github account, click on your profile pic in the upper right corner, followed by 'Settings'.
From here, click on 'SSH and GPG Keys' in the menu on the left, followed by the green 'New SSH Key'. You'll notice in the screenshot that I already have one SSH key in my Github account but can easily add more.
Head back over to your terminal, make sure you're in your .ssh directory and run
pbcopy < id_rsa.pub. This command will copy the contents of you id_rsa.pub file to your clipboard. With your ssh public ready in your clipboard, go back to Github, give your SSH key a name, hit cmd + v to paste in your key, and click the green 'Add SSH key' button.
When you are returned to the previous screen, you should see your new SSH key listed.
If you were running into the 'permission denied' error, or have been needing to enter a password whenever you push a change, you should now be able to push to that repo without any issue. :)