繁体   English   中英

Git 不使用 SSH 密钥 (Windows)

[英]Git does not use SSH key (Windows)

我为我的 GitLab 存储库创建了一个 SSH 密钥,当我使用 SSH 对其进行测试时,它确实有效并要求输入密码:

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

我也用过

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

没有错误。 但是,当我尝试提交并推送到存储库时,Git 要求输入git@gitlab.lrz.de的密码,而不是 SSH 密钥的密码。

根据文档( https://docs.gitlab.com/ee/ssh/ ),您可以在配置文件~/.ssh/config中设置 ssh 密钥。 所以我在这个目录中创建了一个包含以下内容的文本文件:

Host gitlab.lrz.de
 HostName gitlab.lrz.de
 IdentityFile ~/.ssh/id_ed25519

它仍然不起作用,我不确定它是否甚至使用这个配置文件。 我能够在 Linux 服务器上运行所有内容,但不能在这台 Windows 计算机上运行。 我通过命令行和 Pycharm 进行了尝试。

>git --version
git version 2.35.1.windows.1 

您可以在 git 配置中自定义 git 使用的 ssh 命令,然后在使用任何 git 命令时传递您的密钥。

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

在PyCharm中,尝试导航到VCS -> Git -> Remotes ,并确保配置的URL与8820160549088 8881657866088888888888888888888888888888888888888888888888888888888888888(U可以看到8816578602088)(U可以看到88165788888888888888888888888888888888400884008884008840088400208840002.U

当你在 Windows 上有一个或多个 ssh 密钥时,用于推送到单独的 Git 存储库:

c:\用户\anbj\.ssh\配置:

    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

注意不要写冒号(如 c: )。 令人困惑的是,这在运行测试时会起作用,例如

    ssh -vT github.com

...但是一旦您必须执行 Git 推送就会失败

    git push -u origin develop

删除冒号并使用绝对路径对我有用 Git 2.36.0.windows.1 on Windows 11。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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