GitHub: Permission denied (publickey). fatal: Could not read from remote repository

GitGithubGit Remote

Git Problem Overview


I´m having trouble with GitHub. I´ve playing around with a remote repository of Git.

When I now try to make any changes to the remote directory, i.e.

git remote show origin

or

git push -u origin master

I get this error >Permission denied (publickey). fatal: Could not read from remote repository.

> Please make sure you have the correct access rights and the repository exists. Ulrichs-MacBook-Pro:coredatatest ulrichheinelt$ git push -u origin master Permission denied (publickey). fatal: Could not read from remote repository.

I would be happy, when I could start again with a new (empty) remote directory. Or is there a way, to fix this error?

This are my first steps with GitHub, started yesterday... Many thanks in advance!

Edit 1

my settings at https://github.com/UlliH/CoreDataTest/settings

...

Edit 2

too early happy :-(

After setting the SSH and GPG keys, the errors are still the same. :-/

Edit 3

I think that's right so, but still the same...

enter image description here

Git Solutions


Solution 1 - Git

  1. Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email".
  2. Copy the output of cat ~/.ssh/id_rsa.pub to your clipboard
  3. Paste the above-copied output to the form at https://github.com/settings/ssh/new

Update: If you are still facing "ssh: connect to host github.com port 22: Connection timed out", then follow the below steps.

Check the connection, mostly it will time out

$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection timed out

Check the same by providing optional param port

$ ssh -T -p 443 git@ssh.github.com
Hi <user_name>! You've successfully authenticated, but GitHub does not provide shell access.

Update the SSH settings

$ vim ~/.ssh/config
# Add github in the know hosts
Host github.com
  Hostname ssh.github.com
  Port 443

Check the connection, mostly it will connect this time

$ ssh -T git@github.com
Hi <user_name>! You've successfully authenticated, but GitHub does not
provide shell access.

Solution 2 - Git

  • On your GitHub profile there is an Edit Profile button.
    It is located on top-right corner of the webpage.

  • Press it and you will see left Personal Settings menu.

  • Inside that menu find SSH and GPG keys option and press it.

  • You will see an option New SSH key to add new key.

Solution 3 - Git

  1. generate your key

    ssh-keygen

  2. Visualize your keys

    ls ~/.ssh

    id_rsa id_rsa.pub

  3. Start the agent

    eval `ssh-agent`

  4. Add your key to the agent

    ssh-add ~/.ssh/id_rsa

Solution 4 - Git

For me I had to set what host to use what SSH key. In your local machine SSH folder, usually under ~/.ssh create/edit the file called config using your preferred editor like vim or gedit

vim ~/.ssh/config

and add the following with your git Host, HostName, and ssh IdentityFile (your ssh private key file path):

Host gitlab.example.com
    HostName gitlab.example.com
    IdentityFile /home/YOURUSERNAME/.ssh/id_rsa

Solution 5 - Git

I was having the same problem with my ssh connection. I tried to work it through ssh, but couldn't find a working solution for it. So, in that case, I changed my remote URL from SSH to HTTPS. I used the command: $ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git. You can see your remote url changed using: $ git remote -v .

You can find more detail on Here

This will change your remote URL to HTTPS so you will now have to type your GitHub username and password to push your project to the remote repo. I know ssh is easier than HTTPS meaning that you don't have to type out your username and password, but this might be helpful if you didn't find any solution for fixing it through ssh, and you are in a rush to push your code to your repo.

Solution 6 - Git

On Windows I'd the problem when using git in powershell or command prompt. Using Git Bash on Windows solved it for me as it allows for ssh-agent to properly run in the background and allow the SSH credentials forwarding necessary to make this work.

Following the instructions at this link in Git Bash worked for me: https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Solution 7 - Git

For those on Windows and still cannot get this figured out even though following the solutions above. I followed these to make it work.

  1. Go to C:\Users<accountName>.ssh or just do cd to this folder in command prompt
  2. Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email" in the command prompt
  3. Important aspect in the second step is that leave everything to default and add no passphrase too. Keep pressing enter untill the key is generated. I was changing the name of the file and adding passphrase and it wasn't working for me that way. Two files namely id_rsa and id_rsa.pub will be created
  4. Copy the output of type id_rsa.pub or cat id_rsa.pub for linux to your clipboard
  5. Paste this key in the SSH keys under git or bitbucket account
  6. Close the CMD and try using git clone now and it should work

*I didn't use ssh-add id_rsa as it is done to add this private key(id_rsa) into a vault

Solution 8 - Git

I solved the same issue before by adding the file below in the key path.

I have created the keys using git bash (ssh-keygen) and this automatically saved the key into C:\Users\{username}\.ssh folder.

I then created a file "config" without extension and added the code below:

Host *
     PubkeyAcceptedKeyTypes +ssh-rsa

Now, try to clone again using git clone command.

Solution 9 - Git

  1. make sure you have named the "public key" and "private key" files properly; precisely like "id_rsa" and "id_rsa.pub". This is something that you can find in your users/.ssh folder.

  2. add the public key in GitHub

  3. Restart your terminal ( bash supported) and try to clone again

if you have the write access to the repo, you should be good to go after these changes.

Talking from experience (after spending an hour), I could not find any info on any forum that stated that we have to explicitly keep the name of the private and public file as mentioned above.

Happy coding!

Solution 10 - Git

If any of you facing same kind of issue on Bitbucket then here is the solution:

Issue:
------

Demo@L90TQCLQ MINGW64 /u/works (master) $ git clone ssh://[email protected]:5449/rem/jenkinspipeline.git Cloning into 'jenkinspipeline'... [email protected]: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Solution:
Demo@L90TQCLQ MINGW64 /u/works (master) $ cat < ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC99aqMXtBpVvCQb6mezTHsftC3CFY9VOpGaNmckmcTzXoOOWOheeM9V2NTrOWxpbE3UNdL/6ZnmPyv5EI2zPMPstVIP5jAqcmxOvGc2zxy4wHeGTgrP6UaXs2nLScV4E7+rmdaVtSsfOa1i+eU2eg4UnIJpRLtGD6N+hMKJXaXWpGfQ79USiPhPQKDGOz3PeEDggyvHs7HUzaKZpwEeIKYOSDXsvDwTJ6s5uQ30YfX3eoQbAis8TJeQejAmkuu62oSOs5zFJMSTAzakiyXW/xCUsLrnUSzxmBKO2BIA/tSTrqW/Gj0VhDniDFGwGz0K1NfLzfEJLWKvdB2EJWVFjEd [email protected]

Goto: https://bitbucket.internal.abc.com/plugins/servlet/ssh/projects/REM/repos/jenkinspipeline/keys

  1. Add keys Copy/paste the id_rsa.pub key value there:

enter image description here
Done! Now you can able to clone the git repository

KDemo@L90TQCLQ MINGW64 /u/works (master) $ git clone ssh://[email protected]:5449/rem/jenkinspipeline.git Cloning into 'jenkinspipeline'... remote: Enumerating objects: 1146, done. remote: Counting objects: 100% (1146/1146), done. remote: Compressing objects: 100% (987/987), done. remote: Total 1146 (delta 465), reused 0 (delta 0) Receiving objects: 100% (1146/1146), 149.53 KiB | 172.00 KiB/s, done. Resolving deltas: 100% (465/465), done.

Solution 11 - Git

In my short experience using git with linux, I found there were two simple answers to this error.

run these commands in this order

git remote set-url --add origin <https://github.com/username/repo>
git remote set-url --delete origin <[email protected]:username/repo>

This will reconfigure your config file to use HTTPS origin instead of SSH.

now try running push or pull commands.

OR

Reboot your linux VM (if you're using one) and/or host machine. Rebooting has resolved the issue for me more than once.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUlli HView Question on Stackoverflow
Solution 1 - GitShravan40View Answer on Stackoverflow
Solution 2 - GitEvgeny KarkanView Answer on Stackoverflow
Solution 3 - GitWaldeyr Mendes da SilvaView Answer on Stackoverflow
Solution 4 - GitWaqlehView Answer on Stackoverflow
Solution 5 - GitshrillshrView Answer on Stackoverflow
Solution 6 - GitVikash MadhowView Answer on Stackoverflow
Solution 7 - GitRohit SinghView Answer on Stackoverflow
Solution 8 - GitSanoojView Answer on Stackoverflow
Solution 9 - GitVikas PandeyView Answer on Stackoverflow
Solution 10 - Gitnk07View Answer on Stackoverflow
Solution 11 - GitBlusteel408View Answer on Stackoverflow