Skip to content

Instantly share code, notes, and snippets.

@nathansnow1981
Last active November 23, 2021 03:28
Show Gist options
  • Save nathansnow1981/5276293a3215ac05788233444084af6b to your computer and use it in GitHub Desktop.
Save nathansnow1981/5276293a3215ac05788233444084af6b to your computer and use it in GitHub Desktop.
[SOLVED] GitHub 'Permission Denied (publickey)' error when pushing local changes to a repository!

"GitHub 'Permission Denied (publickey)' error when I try to push!"

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash or your choice of CLI (e.g. windows command prompt (CMD), powershell or the Mac Terminal).
  2. Type cd ~/.ssh(cd %USERPROFILE%/.ssh in command prompt). This will take you to the root directory for your SSH keys (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows).
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that authenticate your computer so it can communicate with GitHub, BitBucket, or any other Git based service. Type ls(dir in command prompt) to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the required SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files (if asked for a key filename or password you can just ignore these and press enter). For the curious: the above ssh-geygen command has 3 parts, 1. The command itself ssh-keygen, 2. The -t flag which is for defining the key encryption type (in this case RSA), and 3. The custom key comment which will be appended to the end of the key.
  5. Now, go and open id_rsa.pub in your favorite text editor (you can do this via Windows Explorer or the OSX Finder if you like, typing open . will open the folder). Pro Tip: You can open the key in VSCode by typing code . id_rsa.pub directly into your terminal)
  6. Copy the contents exactly as it appears, with no extra spaces or lines of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys. (In GitHub, go to https://github.com/settings/keys) NOTE: I like to give the SSH key a descriptive name, usually with the name of the workstation I'm on along with the date.
  7. Now that you've added your public key to Github and/or BitBucket, you should now be able to git push from your local machine.

More help available from GitHub on creating SSH Keys and BitBucket Help. Source thanks to Adam Johnson from his gist Fix "Permission denied (publickey)" error when pushing with Git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment