首页 > 解决方案 > GitLab 在添加 SSH 密钥后要求输入密码

问题描述

亲爱的 StackOverflow 社区,

我按照这个( https://gitlab.com/help/ssh/README)指令创建了 ssh-keys 。我将 ED25519 SSH 密钥对添加到网页上的 GitLab 配置文件中,如果我这样做了,ssh -T git@gitlab.com我确实获得了消息Welcome to GitLab, @username!,到目前为止它似乎工作。

git push但是,我想git pull从我的本地存储库到 GitLab,而无需每次都输入我的密码。据我了解 SSH 密钥,这应该正是他们所做的事情,对吧?

编辑:

我的.ssh/config包含:

Host machine                                                                                                                                                                                                   
      User username                                                                                                                                                                                           
      HostName machine.webside.com

.gitconfig包含:

[user]                                                                                                                                                                                                      
        name = John Doe                                                                                                                                                                                   
        email = john.doe@email.com

并且.git/config包含:

[core]                                                                                                                                                                                                      
        repositoryformatversion = 0                                                                                                                                                                         
        filemode = true                                                                                                                                                                                     
        bare = false                                                                                                                                                                                        
        logallrefupdates = true                                                                                                                                                                             
[remote "origin"]                                                                                                                                                                                           
        url = https://gitlab.com/johndoe/projectname.git                                                                                                                                              
        fetch = +refs/heads/*:refs/remotes/origin/*                                                                                                                                                         
[branch "master"]                                                                                                                                                                                           
        remote = origin                                                                                                                                                                                     
        merge = refs/heads/master                                                                                                                                                                           
[branch "branch1"]                                                                                                                                                                                  
        remote = origin                                                                                                                                                                                     
        merge = refs/heads/branch1                                                                                                                                                                  
[branch "branch2"]                                                                                                                                                                                        
        remote = origin                                                                                                                                                                                     
        merge = refs/heads/branch2

标签: gitlabubuntu-18.04ssh-keys

解决方案


.git/config表明您的存储库是通过 HTTPS 访问的,因此使用用户名和密码身份验证。

如果要切换到 ssh,除了添加 SSH 密钥外,还必须更改本地 GIT 配置。

GitHub 有一个关于将 GIT 远程 URL 从 HTTPS 更改为 SSH 的非常详细的教程:https ://help.github.com/en/articles/changeing-a-remotes-url#switching-remote-urls-from- https-to-ssh
那里描述的步骤也适用于 GitLab,除了您的远程 URL 将类似于git@gitlab.com:<repo-url.git>而不是git@github.com:<repo-url>.git

或多或少,您只需要使用git remote set-url origin.


推荐阅读