Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active May 25, 2023 06:20
Star You must be signed in to star a gist
Embed
What would you like to do?
Fix "Permission denied (publickey)" error when pushing with Git

"Help, I keep getting a 'Permission Denied (publickey)' error when I 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 (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (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 tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls 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 SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files.
  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).
  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. 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, try to git push again and see if it works. It should!

More help available from GitHub on creating SSH Keys and BitBucket Help.

@Volodymyr-Shabala
Copy link

Hello, I think I’m not the correct audience for this mail chain. If yes , please exclude me from this call. Best Regards, Vani From: Ekta Arora @.> Date: Thursday, 29 September 2022 at 07:30 To: adamjohnson @.> Cc: Vani Swamy @.>, Comment @.> Subject: Re: adamjohnson/publickey-git-error.markdown @Ekta17 commented on this gist.

________________________________ this works! one extra step is to add the rsa key in your path: touch ~/.ssh/config vi ~/.ssh/config paste following in this file and change location of the rsa file: Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa ssh-add -K ~/.ssh/id_rsa — Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2F5682757%23gistcomment-4319125&data=05%7C01%7Cvani.swamy%40ingka.ikea.com%7Cd6fe2548628e4f8db1bc08daa1dbc5a9%7C720b637a655a40cf816af22f40755c2c%7C0%7C0%7C638000262552187699%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=rwkWT3KspXxH6erjIjL9gwD3kDudmWZU0iw4KVKwpGc%3D&reserved=0, or unsubscribehttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYDSDCEK2WYHRR7PWI4B62TWAUSQVANCNFSM4HI4HAHA&data=05%7C01%7Cvani.swamy%40ingka.ikea.com%7Cd6fe2548628e4f8db1bc08daa1dbc5a9%7C720b637a655a40cf816af22f40755c2c%7C0%7C0%7C638000262552187699%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=SqRZnNCXjvPN0YuNvdd1WWp23nQELzxKeB5Hjdphjis%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

Hi. There is an unsubscribe button in the email you get that you can press to stop receiving updates from this thread.

@arifbangashse
Copy link

How did you fix it with another user?

@a-malacarne
Copy link

I got the same error when following the key generation guid for Mac on Github docs: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key

The problem for me was the entry in .ssh/config which has to be for host *github.com and not *.github.com as the guide says since *.github.com does not match the ssh domain git@github.com:your-user.... So Git didn't even use the key when trying to push to Github.

@BotanicalAmy
Copy link

THANK YOU! Finally connected PyCharm because of this.

@GeorgeLukoff
Copy link

thanks, you are awesome!!!

@eliascreates
Copy link

This helped. Thank you so much 🙏

@appaloosa-marketing
Copy link

Thank you!

@kdaysal
Copy link

kdaysal commented Nov 24, 2022

Bravo!! I was getting the Permission Denied (publickey) error when trying to execute npm run deploy to publish an app to gh-pages for the first time. Kept going in circles on different forums without much luck. One suggestion was to change the remote's URL from SSH to HTTPS - which I tried, and it worked ONCE (published to gh-pages successfully) - and then some time later I tried to publish new changes and it failed with the same error as before. Such a headache, but the solution described here worked like a charm after I switched my remote URL back to SSH and followed the steps. Thank you so much! :)

@nyamwaya
Copy link

nyamwaya commented Dec 1, 2022

worked for me. had to rename files to id_rsa respectfully

@theo-carr
Copy link

Thank you so much! fixed a problem I was having for a few days...

@Manimit10
Copy link

Very helpful, thanks

@Zyanvel
Copy link

Zyanvel commented Dec 15, 2022

Thank you SO much!

@johannesrave
Copy link

using WSL, this DID solve my problem for windows-based git. however, using WebStorm and a project based in the linux file system, it still wasn't enough and I couldn't get it to work for several months. the solution finally came by wrapping the git that WebStorm (or IntelliJ etc) uses for WSL projects in a bash script that starts the respective ssh-agent, see this thread:
https://youtrack.jetbrains.com/issue/IDEA-275823/Git-from-WSL-Support-SSH-agent-from-WSL

@bassigbc
Copy link

Thanks, it works with Ubuntu on WSL2

@harsh-kp
Copy link

Thanks man, i named my keys id_"some_custom_name" but i renamed it to rsa and it started working fine.
Still i didnt get why it didnt work for custom name. I added the identity in the config file

@santosh2818
Copy link

This is very helpful. Easy steps saved lots of trouble.

@steveninety
Copy link

steveninety commented Jan 3, 2023

WARNING!
Powershell terminal users!

You'll keep getting this error even after following the outlined instructions. It only works in the Bash/Cygwin terminal. In Cygwin, I ran:

ssh -T git@github.com

Result:

Hi steveninety! You've successfully authenticated, but GitHub does not provide shell access.

Find the comment by @2bam where he explains how to get rid of the error in Powershell.

@eprasetyaw
Copy link

thanks. it's work master.

@ranjankhanna
Copy link

I am also running into same issue on my mac. I tried above solution but didn't work. Following is my ~/.ssh/config file:

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

Can anyone please help!

@TifSecberus
Copy link

TifSecberus commented Jan 10, 2023

I have this issue and need help! My config file is set up correctly, I can connect to GitHub using the command ssh -T@github.com. I ran the command ssh -vT git@github.com and was able to see my public key connect successfully. However when I run the command ssh-add -l -E sha256 I receive this error message "The agent has no identities." I can not understand how this is possible and any help would be greatly appreciated as I have been working on this issue for 2 weeks. Using a Mac.

@delacrucen
Copy link

Thanks!

@mbgoodguy
Copy link

Thanks! It's really works! <3

@soscarlos
Copy link

It worked! Thanks!

@bitbyalex
Copy link

Thank you! Finally it worked!

@starthecode
Copy link

Not working at all

@LiuXH98
Copy link

LiuXH98 commented Feb 14, 2023

Thank you so much!!!!!

@ArielRobson
Copy link

Thanks!

@lanbufan
Copy link

Thank You! Works like a charm.

@Coding-Noob30
Copy link

you saved me

@FREEDOCTOR1986
Copy link

Thanks man, i named my keys id_"some_custom_name" but i renamed it to rsa and it started working fine.
Still i didnt get why it didnt work for custom name. I added the identity in the config file

hello sir, I also have the same issue how can I rename ? please help

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