I created a SSH key for my GitLab repository and when I test it with SSH it does work and asks for the passphrase:
>ssh -T [email protected]
Enter passphrase for key 'C:\Users\[username]/.ssh/id_ed25519':
Welcome to GitLab, @000[...]!
I also used
>git remote set-url origin [email protected]:000[...]/project.git
without errors. However, when I try to commit and push to the repository, then Git asks for the Password of [email protected]
and not for the passphrase of the SSH key.
According to the Docs (https://docs.gitlab.com/ee/ssh/), you can set the ssh key in the config file ~/.ssh/config
. So I created a text file in this directory with this content:
Host gitlab.lrz.de
HostName gitlab.lrz.de
IdentityFile ~/.ssh/id_ed25519
It still does not work and I am not sure if it even uses this config file. I was able to get everything running on a Linux server but not on this Windows computer. I tried it via command line and via Pycharm.
>git --version
git version 2.35.1.windows.1
@kipy is nearly correct...
The issue is that git bundles its own SSH executable with it, and as in Windows (at least historically) the native OpenSSH client was optional, git won't acknowledge it by default. As a result of this, when you're doing things like:
ssh-add <my-key>
In Powershell (or whatever), that's not actually the SSH client that your git commands are using.
This complicates things further as, like me, you'll type:
ssh -T [email protected]
See the "Success!" message, and then scratch your head, wondering why git clone [email protected]:<username>/<repo>.git
gives a (public key)
error. I guess the clue is pretty clear, but not obvious... In one you run ssh
directly, the other one you run git
, which then calls 'ssh' by proxy.
The correct command to solve this, is to tell your git installation (via its config) which version of SSH you want it to use, so:
git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe
This will then prompt all git
commands to use the same SSH client that your ssh
command does.
This also allows you to use different keys for different remotes, as well as working in all terminals/applications.
NOTE: The above assumes that OpenSSH for Windows is installed via the Windows Optional Programs process, which is most likely the case, but just to be sure, you can open a PowerShell window, and run:
(Get-Command ssh).Path
Which will output something like:
C:\Windows\System32\OpenSSH\ssh.exe
If it doesn't output that, then replace the last part of the git config
command above with this output instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With