Skip to content

Instantly share code, notes, and snippets.

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 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 in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "". This will create both id_rsa and files.
  5. Now, go and open 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 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.

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.)

Copy link

nbomasi commented Jul 5, 2022

Thanks, it solve my issue

Copy link

pauGuarch commented Jul 6, 2022

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

Copy link

knkailash commented Jul 6, 2022

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

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.

Copy link

pbatsaihan commented Aug 3, 2022


Copy link

EmilyYChan commented Aug 12, 2022

Thank you for the great explanation!

Copy link

TonniPaul commented Aug 19, 2022

it worked for me but now I have to input my phrase before i could push anything


Thanks, it worked.

Copy link

mhd-hi commented Aug 31, 2022

It didn't work for me. I had to rename my .pub public file and the private file to id_rsa and it works fine now 🎉🎉 Have a great code!

Copy link

apprajapati9 commented Sep 8, 2022

what if I don't wanna have the default name like id_rsa. pub? what would be the setup or changes that I have to do if I change key names?

Copy link

2bam commented Sep 11, 2022

If you:

  • don't want to be forced to use id_rsa
  • don't want to input a passphrase on each call to git
  • don't want to use Git Bash because you want to use it from CMD/PowerShell directly
    you need some troubleshooting to use git installation executables and a prior setup:

First of all thanks for this gist, it helped me out when I was at a total loss!!!

1. Start or get currently running agent into env vars (PID and socket/pipe file).
It should be in PATH, otherwise find it at C:\Program Files\Git\cmd
This command undocumented in the FAQ will spawn a child CWD with the dynamic env vars set. Use it, if you close it will leave the agent on but git calls won't work as wanted.

2. Make sure you're using the ssh executables from the git installation
In PowerShell: Get-Command ssh (this is like which ssh in Linux)
You're probably not using that, so point there before anything else.
In CWD: set PATH=C:\Program Files\Git\usr\bin;%PATH%
In PowerShell: $Env:PATH = "C:\Program Files\Git\usr\bin;$Env:PATH"

3. Only after that load keys using git installation's ssh-add:
ssh-add <PATH_TO_KEY>
And enter the passphrase.

Then you can use git directly from CWD/PowerShell without (so much) troubles. Not only git push but also git clone, etc.
I needed this for some automation and didn't want to spawn a Git Bash per git call, neither wanted to run inside Git Bash.

Copy link

andresernajoto commented Sep 17, 2022

Thanks, now it's working correctly!

Copy link

Tadesse-Alemayehu commented Sep 18, 2022

Thank you brother, It's working. And for someone curious like me. If you don't like to name your ssh id_pub. You need to re-install git since most of the time the problem is related to the git you have installed or how you install it. For me the problem was, I install git on a different user account and try to use it in another one. 😎

Copy link

joel-dev22 commented Sep 21, 2022

I added public key to Github...But still i am having the error..Can you explain why brother?

Copy link

yusufkarakaya commented Sep 28, 2022

it works! thank you.

Copy link

Ekta17 commented Sep 29, 2022

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

Copy link

Volodymyr-Shabala commented Oct 6, 2022


Copy link

pfabiszewski commented Oct 6, 2022

Thank you!

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