38

I created a new remote repository and tried to use git push -u origin master command to push my local files into the new repository the first time after I add it and commit it. However, it pops up this git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.. How can I fix this fatal error?

I've tried this
How to solve Permission denied (publickey) error when using Git? It seems that the question in this link happens in the first time using git. I've used my git for a while, do I still need to follow this solution? Can anyone gives me a more specific solution?

This the fatal error that I got

C:\Users\ASUS\Desktop\React-Practice App\my-app>git status
On branch master
nothing to commit, working tree clean

C:\Users\ASUS\Desktop\React-Practice App\my-app>git push -u origin master
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.
Jessie
  • 787
  • 1
  • 14
  • 27
  • Possible duplicate of [GitHub Error Message - Permission denied (publickey)](https://stackoverflow.com/questions/12940626/github-error-message-permission-denied-publickey) – phd Aug 31 '19 at 11:18
  • https://stackoverflow.com/search?q=%5Bgit%5D+Permission+denied+publickey – phd Aug 31 '19 at 11:18

6 Answers6

50

You’re accessing GitHub through SSH. First generate an SSH key pair; then add the public key to GitHub.

Generate key pair, github prefers the "Ed25519 algorithm"

ssh-keygen -t ed25519 -C "your_email@example.com"

It still allows using rsa for systems that don't support Ed25519

ssh-keygen -t rsa -b 4096 -C “youremail@example.com”

See more at https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent and https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account

Community
  • 1
  • 1
Moazzem Hossen
  • 1,709
  • 14
  • 26
  • 4
    I've use this command ```ls -al ~/.ssh``` to check out whether I have a existing ssh key. The result is I already have one. Therefore, why do I need to generate a new one, if I already have one? – Jessie Aug 31 '19 at 02:29
  • 7
    Navigate to github profile > Settings > SSH and GPG keys > New SSH key. In the text-field paste the content of `~/.ssh/id_rsa.pub` file. See more at the second link in answer. – Moazzem Hossen Aug 31 '19 at 02:38
  • 3
    I already had a ssh key, and it is encrypted. So I needed to unlock it first by adding it (on a mac) like this: `ssh-add ~/.ssh/id_rsa`, where `id_rsa` is the secret key of your ssh. Then type in my encryption password for it. – Knogobert Dec 29 '20 at 14:23
  • On **Ubuntu** after running `ssh-add id_rsa` I received the error _could not open a connection to your authentication agent_ If you ever run into this . Run 1) `exec ssh-agent bash` 2) `ssh-add ` .) [video](https://www.youtube.com/watch?v=_px5jRgxF3Q) – MattJamison Nov 24 '21 at 19:06
  • For some weird reason, suddenly I can't reach gh with ssh any more, following the step @Knogobert suggested helps me. So weird. Because I remember I did it already, for sure. – Sang Dang May 04 '22 at 07:39
13

Had the same issue even after adding ssh keys on github.

Fixed it by running this command

ssh -vT git@github.com

Eric
  • 742
  • 10
  • 25
  • 3
    Thank you very much! It showed the error logs and found out that git uses specific path (~/.ssh) and filename (id_) when trying to authenticate. I shouldn't have renamed that. – Mon Apr 05 '21 at 05:51
  • 1
    Thanks, this solved my problem with the Github today. – Arefe Aug 19 '21 at 08:12
8

I had this problem because I used a non-standard location for my key like @Mon did. In my case, I did not use the default file name because I have more than one key on my system. So it's not possible to resolve it simply by moving or renaming.

The solution is to edit or create ~/.ssh/config (OpenSSH docs for config and Configuring the Location of Identity Keys) to contain the following:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-key
  • Do not edit line 3 to reflect your own username. The username should be git (see github docs).

  • Edit line 4 to reflect the location of your key.

If you already have a config file, it would be worthwhile having a look at what's there before futzing around. It never hurts to make a copy before editing.

The above assumes that you have created the key, put it on github, that you actually do have permissions, and everything else is as it should be, but still encountering error.

stichResist
  • 81
  • 1
  • 1
  • after failing to push i tried many solutions but this worked for me also after transforming my key to new open ssh format with putty gen – klys Dec 02 '21 at 07:50
1

When I had this problem, I ended up having a typo in my git remote origin url. Maybe you can double check that everything is spelled correctly without any additional unintended keystrokes?

shrfu31
  • 11
  • 2
0

I was having the worst time getting this setup and had the same error pop up.

How I fixed my issue was not having the .git folder as root and using another user to access it by using sudo. I changed the folder permission to the user:

chown -R user:user .git/
buddemat
  • 3,262
  • 10
  • 15
  • 39
Digi Jeff
  • 149
  • 1
  • 3
  • 12
0

On Windows there are sometimes multiple version of SSH installed.
It results in conflict when using git:
git@github.com: Permission denied (publickey).
To resolve it, just point the correct version in environment variable:

setx /m GIT_SSH C:\Windows\System32\OpenSSH\ssh.exe

vSzemkel
  • 488
  • 4
  • 9