简体   繁体   中英

Git does not use SSH key (Windows)

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 git@gitlab.lrz.de
Enter passphrase for key 'C:\Users\[username]/.ssh/id_ed25519':
Welcome to GitLab, @000[...]!

I also used

>git remote set-url origin git@gitlab.lrz.de:000[...]/project.git

without errors. However, when I try to commit and push to the repository, then Git asks for the Password of git@gitlab.lrz.de 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 

You can customize the ssh command used by git in the git config and then pass your key when using any git command.

git config core.sshCommand "ssh -i ~/.ssh/id_ed25519"

in PyCharm try navigating to VCS -> Git -> Remotes and make sure that the URL configured there is aligned with the SSH URL (u can see the ssh URL when navigating to the repo via the browser and clicking on the clone button)

When you have one or more ssh-keys on Windows, for pushing to separate Git-repositories:

c:\Users\anbj\.ssh\config:

    Host github.com
        Hostname github.com
        User git
        IdentityFile /c/Users/anbj/.ssh/id_ed25519_client_A

    Host otherhub.com
        Hostname otherhub.com
        User git
        IdentityFile /c/Users/anbj/.ssh/id_ed25519_client_B

NOTE Do not write a colon ( as in c: ). Confusingly this will work when running a test such as

    ssh -vT github.com

... but will fail once you got to do a Git push as in

    git push -u origin develop

Removing the colon and using an absolute path is what worked for me using Git 2.36.0.windows.1 on Windows 11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM