9

I came across a weird issue while cloning a git repository using ssh. I have the ssh setup:

ssh -T [email protected]
Hi yusufali2205! You've successfully authenticated, but GitHub does not provide shell access.

I am using the right clone url and have access to the repo I want to clone. But getting error:

➤ git clone [email protected]:<some-org>/<repo>.git
Cloning into 'project'...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

There is no other message to debug what is wrong with my ssh setup or git configuration.

4 Answers 4

6

I encountered the same issue, but in my case it was because I named my key files using a custom name (e.g., my_key and my_key.pub), and not letting ssh-keygen use the default ones, which are:

  • id_rsa
  • id_dsa
  • id_ecdsa
  • id_ed25519
  • id_ed25519_sk
  • id_xmss

If you add the custom named key using ssh-add (as many tutorials say to do), then the command ssh -T [email protected] works perfectly, but not the git clone command.
This is due to the fact that git looks only for keys with default names when using ssh.

I was able to determine this by adding to the global .gitconfig the following property:

[core]
    sshCommand = ssh -vvv

which basically spits out all the log messages when you try to clone a repository (or other git operations) when using ssh.

Note: I'm on Windows 10, and it could be that the issue is also related on how to git works on Windows. I have not tested this on *nix systems.

1
  • 1
    Thanks for the core.sshCommand in .gitconfig. Very helpful! git config --global core.sshCommand "ssh -vvv"
    – Rich C
    Commented Aug 25, 2023 at 4:45
5

I found out there was an entry in my .gitconfig which was replacing ssh with https.

[url "https"]
    insteadOf = git

I might have accidentally added this entry while using some tool. So the clone command was actually using the url [email protected]:<some-org>/<repo>.git

After removing the above entry from .gitconfig the problem was resolved.

2

In my case, I had to delete ~/.ssh/known_hosts file, so that while cloning, it will remake that file. After this, it worked

1
  • My case was slightly different to OP. After an upgrade of our GitHub server the ssh clone worked fine but the https clone would hang and give 404, eventually. I think the issue is under the hood somewhere the ssh connection was waiting for the user to accept the new id of the server. I didn't delete the whole file I just removed the single entry for our GH server from the known_hosts file.
    – Martin
    Commented Sep 28, 2021 at 12:55
0

I was having the same problem and researching I tried something and I got it. Go to your .ssh directory, open the file known_hosts with the notepad, right at the beginning it has "github.com" ... insert SSH: at the beginning of github.com and it will look like this: "SSH: github.com" and save, try again. It worked for me!

Not the answer you're looking for? Browse other questions tagged or ask your own question.