Github SSH works but clone using ssh fails

Viewed 4950

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

ssh -T git@github.com
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 git@github.com:<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

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 git@github.com 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.

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

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!

Related