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.

@pbatsaihan
Copy link

pbatsaihan commented Aug 3, 2022

Thanks

@EmilyYChan
Copy link

EmilyYChan commented Aug 12, 2022

Thank you for the great explanation!

@TonniPaul
Copy link

TonniPaul commented Aug 19, 2022

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

@Murillo-Henrique-Monteiro-Nacfur

Thanks, it worked.

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

@apprajapati9
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?

@2bam
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).
start-ssh-agent.cmd
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.

https://gist.github.com/2bam/33347ff54be8088943450f20323fde53

@andresernajoto
Copy link

andresernajoto commented Sep 17, 2022

Thanks, now it's working correctly!

@Tadesse-Alemayehu
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. 😎

@joel-dev22
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?

@yusufkarakaya
Copy link

yusufkarakaya commented Sep 28, 2022

it works! thank you.

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

@Volodymyr-Shabala
Copy link

Volodymyr-Shabala commented Oct 6, 2022

Thanks!

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