Skip to content

Instantly share code, notes, and snippets.

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.

@ecg-sniper
Copy link

ecg-sniper commented Feb 6, 2022

This time I have to spend a minute to thank you for the good explanation.

@mariaclarahl
Copy link

mariaclarahl commented Feb 18, 2022

Works for me, thank you very much!

@nielvid
Copy link

nielvid commented Feb 19, 2022

@rollanda21
Copy link

rollanda21 commented Feb 21, 2022

Thanks a lot! This worked for me.

@ekjamu
Copy link

ekjamu commented Mar 3, 2022

Thank you

@johannesrave
Copy link

johannesrave commented Mar 5, 2022

Didn't work for me unfortunately. I'm still being asked to enter my ssh passphrase on WSL2.

@thirrukumar
Copy link

thirrukumar commented Mar 7, 2022

No solution for me. Finally it works after deleting contents in config file in .ssh.

@NickDulvy
Copy link

NickDulvy commented Mar 12, 2022

Adam, This solved a lot of github woes for me. Thanks.

@malikmukhtar
Copy link

malikmukhtar commented Mar 16, 2022

It works, Thanks

@centratelemedia
Copy link

centratelemedia commented Mar 17, 2022

my problem solve here, thanks

@rocketxl5
Copy link

rocketxl5 commented Mar 24, 2022

It worked! Thank you

@albuquerque-rafael
Copy link

albuquerque-rafael commented Mar 28, 2022

thanks!!

@Naboum
Copy link

Naboum commented Apr 5, 2022

You're a life savior, thank you

@Gio-Ayiman
Copy link

Gio-Ayiman commented Apr 11, 2022

Thanks guy!

@nhut3110
Copy link

nhut3110 commented Apr 18, 2022

thank you, it worked for me!

@berkayalan
Copy link

berkayalan commented Apr 29, 2022

Thanks you, it also worked to me!

@jharris-uncomn
Copy link

jharris-uncomn commented May 5, 2022

VERY helpful for this noob. Thanks!

@gconnect
Copy link

gconnect commented May 8, 2022

Thanks. This worked for me

@9h057
Copy link

9h057 commented May 11, 2022

Face palm. Thanks worked like a charm.

@OscarGabriel07
Copy link

OscarGabriel07 commented May 21, 2022

¡Funcionó, muchas gracias!

@nelsonpoon
Copy link

nelsonpoon commented May 24, 2022

Thanks. This worked for me.

@vaniswamy
Copy link

vaniswamy commented May 25, 2022

Thanks. great help.

@mr-new-0509
Copy link

mr-new-0509 commented May 25, 2022

Awesome! I appreciate you.

@jemilehin
Copy link

jemilehin commented Jun 30, 2022

Thanks! worked for me.

@Xylopyrographer
Copy link

Xylopyrographer commented Jul 2, 2022

FWIW, on macOS, also need to add the private key to the SSH Agent as per this stack overflow link

By doing this in terminal:
ssh-add --apple-use-keychain ~/.ssh/id_rsa

(Note the command above is different from that provided in the link. It jhas been updated to reflect current version of ssh-add.)

@nbomasi
Copy link

nbomasi commented Jul 5, 2022

Thanks, it solve my issue

@pauGuarch
Copy link

pauGuarch commented Jul 6, 2022

After trying many different solutions this one finally worked. Thanks a lot!

@knkailash
Copy link

knkailash commented Jul 6, 2022

Works like a charm, many thanks for this solution suggestion.

@shazib2022skipq
Copy link

shazib2022skipq commented Jul 7, 2022

Bro, youre a lifesaver. I wasted so much time on setting up the credential manager, but finally your post helped me figure this out. Thanks a lot and lots of love.

@pbatsaihan
Copy link

pbatsaihan commented Aug 3, 2022

Thanks

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