2

So, I've followed this tutorial on how to Setup SSH for github with Windows CMD and all was working fine until I went to clone a repo with

git clone git@github.com:{myusername}/{myrepo}.git

Where I get

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.

Even when I run ssh -T git@github.com I get the expected message telling me I'm authenticated.

After scratching my brain for a while, I decided to try it on git bash.

First thing I noticed was that running

 ssh-add -l

in git bash, I was getting The agent has no identities. but when I run the same command on Windows CMD I get all my SSH keys?

So, after adding my ssh key in git bash I was able to clone my repository.

So, why is it only on git bash I can do this and not on the cmd or powershell? Is it something to do with what seems like they are using different ssh agents? How can I sync them together if that is the case?

Furthermore, when I run the following command

ssh -Tv git@github.com

with the cmd I get

debug1: identity file C:\\Users\\{myuserdirectory}/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory

but with git bash I get

debug1: identity file /c/Users/{myuserdirectory}/.ssh/id_rsa type 0

Another difference is that in windows cmd I don't get any instances of debug1: Will attempt key: ....

When I exit git bash and open up another git bash terminal, running ssh-add -l again, it returns The agent has no identities. even after I added it before, it's like it only persists for each session, which also isn't desirable.

Any help with this would be greatly appreciated!

mcclosa
  • 943
  • 7
  • 29
  • 59

3 Answers3

3

Probably you were right and they were using different ssh-agents. I had exactly the same problem and this answer helped me a lot: https://stackoverflow.com/a/40720527/6486458

By default git refers to its own ssh in C:\Program Files\Git\usr\bin. I added GIT_SSH environment variable and set it to C:\Windows\System32\OpenSSH\ssh.exe. This prevents inconsistency between the versions of ssh. After that git started to work as expected from both Git Bash and Windows cmd.

From git documentation:

GIT_SSH, if specified, is a program that is invoked instead of ssh when Git tries to connect to an SSH host. It is invoked like $GIT_SSH [username@]host [-p <port>] <command>.

See also this answer: https://stackoverflow.com/a/8713121/6486458

id.bobr
  • 940
  • 7
  • 8
1

Looks like your ssh-agent is not running or not recognize your ssh key

try this:

# add the default ~/.ssh keys to the ssh-agent
ssh-add

# restart the ssh-agent
eval $(ssh-agent)

# On windows:
start-ssh-agent

ssh-add

ssh-add adds RSA or DSA identities to the authentication agent, ssh-agent.
When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa and ~/.ssh/identity.

Alternative file names can be given on the command line

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • That is what I was using for git bash, but I get `'eval' is not recognized as an internal or external command,` in the cmd? – mcclosa Sep 01 '19 at 00:14
  • Execute it in git bash and try again the CMD. what is the result? – CodeWizard Sep 01 '19 at 00:15
  • I have updated my answer for you for windows with `start-ssh-agent` – CodeWizard Sep 01 '19 at 00:16
  • Tried running in bash and execting in the CMD with the same result. Also, running `start-ssh-agent` then `ssh-add -l` is returning `Error connecting to agent: No such file or directory`. I have no idea why? – mcclosa Sep 01 '19 at 00:19
  • No joy in powershell either, still get the same result when trying to clone a git repo... – mcclosa Sep 01 '19 at 00:26
  • In power shell (run as admin) execute this before: `Get-Service -Name ssh-agent | Set-Service -StartupType Manual` – CodeWizard Sep 01 '19 at 00:26
  • Yeah, that's how I did it via Powershell to begin with https://stackoverflow.com/a/53606760/3113377, I'm really at a loss here, everything works right up until I clone a repo. Like, it even says I'm authorized? Anything to do with the extra backslashes in the directory references from running `ssh -Tv git@github.com`? No idea how they got there in the first place to be honest. – mcclosa Sep 01 '19 at 00:35
0

There is a weird bug on Windows if you install Git bash. Open Command prompt, and do

ls ~/.ssh

if you find this folder already created, then copy the public and private key from your user folder to this path:

cp C:\Users\username\.ssh\id_* ~/.ssh/

For some reason, windows command prompt creates this path the first time you do a git clone, and after that it just requests for git@gitlab / git@github password.

Sriram Murali
  • 5,804
  • 2
  • 26
  • 32