Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active April 18, 2024 01:00
Show Gist options
  • Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
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.

@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!

@TiffLSmith
Copy link

TiffLSmith 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

@gowthamkumar12
Copy link

I have an issue I have created a repo and I am trying to clone the repo using ssh, for that I have setup everything up, but when I am trying to clone the repo I am facing this.

`git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.`

I don't know where I am going wrong. someone please help me with this

@Divya0319
Copy link

Divya0319 commented Jun 12, 2023

I have an issue I have created a repo and I am trying to clone the repo using ssh, for that I have setup everything up, but when I am trying to clone the repo I am facing this.

`git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.`

I don't know where I am going wrong. someone please help me with this

Make sure you have created public and private key pairs properly, by following the steps described in SSH key generation official docs, and also make sure you uploaded your public key in "SSH Keys" section of your GitHub account. I have attached two screenshots from my GitHub account for your reference.
Screenshot 2023-06-12 163242
Screenshot 2023-06-12 163451

@gowthamkumar12
Copy link

gowthamkumar12 commented Jun 12, 2023

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T git@github.com"

@Divya0319
Copy link

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T git@github.com"

For registering newly created SSH keys, you must register your keys (private keys) to SSH agent, instead of requesting shell access of GitHub.

@josehricardo
Copy link

This procedure solved my problem! thanks!!!

@tharaka16
Copy link

Worked like a charm. thank you!!

@bvercelli99
Copy link

Thanks!!

@Vil94
Copy link

Vil94 commented Aug 22, 2023

What a lifesaver, thank you!!

@iamkayshow
Copy link

Thanks it worked!

@pjkitsune
Copy link

Thank you so much! I was stumped!

@Yunishello
Copy link

Thanks so much it worked

@emil-raubach
Copy link

Thank you!

@ScorpQ
Copy link

ScorpQ commented Nov 1, 2023

Thank youuu

@roziqjon1
Copy link

l;,
l

@chronkituss
Copy link

Thank you, it worked!

@sagar03d
Copy link

sagar03d commented Jan 2, 2024

I was having this error on ubuntu, do not forget to add 'sudo' while running ssh-keygen command

@devonkinghorn
Copy link

I had this problem but only with docker-compose build with a git context. Git clone and push still worked though

For some reason when I ran ssh-add, it magically worked. No clue why but hopefully it helps someone.

$ ssh-add ~/.ssh/id_rsa

@HarshitJoshi9152
Copy link

Thank you bro, needed this

@Cegt25
Copy link

Cegt25 commented Apr 18, 2024

Gracias, esta solución me funciono perfectamente

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