Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active October 12, 2024 21:27
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.

@AzwadAbid
Copy link

Thanks. Worked for me.

@rajushikha1
Copy link

Excellent man. Thank you so much. It worked.

@skazhiev
Copy link

Thanks! Worked for me

@royjjay
Copy link

royjjay commented Aug 19, 2020

I've tried practically everything on this page on my Mac and I'm still getting the same error message. Do I make the SSH key for the result of 1.) whoami, 2.) my github username, or 3.) my email address? I've tried all 3 with the same result....
UPDATE: Finally found the answer here -> https://stackoverflow.com/questions/10116373/git-push-error-repository-not-found
Had to remove the origin and re-add with "git remote add origin https://USERNAME:PASSWORD@github.com/username/reponame.git"

@jainruchika
Copy link

@ProgrammerRomero
Copy link

Saved my life!!!

@cloud-henrique
Copy link

Thanks man, saved my life <3

@VenturaFranklin
Copy link

For me Windows did not know what ~ was even after setting HOME environment variable did not help.

Only after setting ~ environment variable to the right path (C:\Users{username}) did it work.

@shravanchk
Copy link

Thanks for the solution, also we need to check the "allow write access" check box after adding the ssh key

@Almorisson
Copy link

Almorisson commented Sep 12, 2020

Host github.com
HostName github.com
User git
IdentityFile /Users/myusername/.ssh/my_github_ssh_private_key_registered_on_github

This solution works fine on MacOSX. Just put the content below in the /etc/ssh/ssh_config and run this : source ~/.zshrc or source ~/.bashrc.

Thanks to @shravanchk

@DamirPecnik
Copy link

THX

@ZOALSD
Copy link

ZOALSD commented Sep 18, 2020

thanks

@MahdiGharooni
Copy link

you may have test by ssh key, try Https

@darcy-vitacca
Copy link

thanks!

@DrIanGregory
Copy link

Complete rubbish.
Waste of time.

@adrian-miasik
Copy link

Worked for me. Many thanks!

@thomasvilches
Copy link

Thanks!!!! It worked!

@soumya1303
Copy link

worked for me at first attempt...thank you !

@AnilkumarAEC099
Copy link

Thanks! Worked for me

@Makav3li94
Copy link

thanks alot, we lazy programmers had problem with this shit and never actualy learnt how to solve it. i'pin this shit to browser :)

@melissahuertadev
Copy link

Omg! thank you!! I forked a repo and needed to fetch from the original repo to mine, and I got this message

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

This post helped me! πŸ™πŸ½

@OverlordsIII
Copy link

Thank you sm this helped me out a lot!

@mari967
Copy link

mari967 commented Oct 16, 2020

Thanks! it worked perfectly for me πŸ‘

@NoailletasJordan
Copy link

Thank you :)

@KevinvFeng
Copy link

Many thanks!!!!

@Mahmud-Alam
Copy link

finally!!! you saved me hours. thanks man πŸ˜ƒπŸ˜ƒπŸ˜ƒ

@TusharManohar
Copy link

In my case I have created ssh key in root user mode.
I deleted all keys and created new without sudo and worked for me

@FutureTechZW
Copy link

none of the above worked for me. but then I tried the following:
$ eval ssh-agent -s
$ ssh-add ~/.ssh/id_rsa
and it worked!

Thanks this worked for me

@Omarjabaly
Copy link

You might have to create a config file (yeap, extension-less) under ~/.ssh/config with the following contents

Host bitbucket.org
    IdentityFile ~/.ssh/id_rsa_your_bitbucket_private_key

That immediately solved my problem!

The ssh was working fine with github but when i used it over gitbash without ssh-agent it stopped working completely even with ssh-agent.
when I added the config file above it worked again. Thanks

@b01
Copy link

b01 commented Dec 3, 2020

Thanks @renatoargh

You might have to create a config file (yeap, extension-less) under ~/.ssh/config with the following contents

Host bitbucket.org
    IdentityFile ~/.ssh/id_rsa_your_bitbucket_private_key

Of course I modified it for Github:

Host github.com
    IdentityFile ~/.ssh/id_ed25519_github.com

This is what worked for me after I moved to a new laptop with Windows 10 (202H). I assume its because ~/.ssh/id_ed25519 is used for another service and I did not have the "id_ed25519.pub" key on Github.com. So adding the config tells Git what SSH private key to use for verification when connecting to Github.com.

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