mcclosa
mcclosa

Reputation: 1455

git clone with SSH only working in Git Bash not on Windows CMD

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 [email protected]:{myusername}/{myrepo}.git

Where I get

[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.

Even when I run ssh -T [email protected] 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 [email protected]

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!

Upvotes: 2

Views: 7608

Answers (4)

Sōōng
Sōōng

Reputation: 67

You may need to verify if your git client is using SSH or credential, for my case, credential is used by default.

So, try to run, (or omit the --global if you use a dedicated config for some certain repo)

git config --global --list

Make sure core.sshcommand=C:/Windows/System32/OpenSSH/ssh.exe is listed, otherwise add it by running

git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe 

Just a few more context, if your ssh -T [email protected] command welcomed you, but you still get the denied error whenever clone or pull or fetch, most likely it is caused by git client not using SSH at all. If ssh -T ... failed, then SOF some ssh related topics

Upvotes: 0

Sriram Murali
Sriram Murali

Reputation: 6234

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.

Upvotes: 0

id.bobr
id.bobr

Reputation: 1000

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

Upvotes: 7

CodeWizard
CodeWizard

Reputation: 142014

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

Upvotes: 1

Related Questions