首页 > 解决方案 > 无法在 github 操作中运行 git clone

问题描述

我设置我的密码和用户名如下:

git config --global user.email $git_email
git config --global user.name $git_username
git config --global user.password $git_password

其中 $git_email、$git_username 和 $git_password 指向正确的环境变量。

Now when I try to run git clone I get the following error: 
Cloning into 'certificates'...
fatal: could not read Username for 'https://github.com': Device not configured

同样的方法在本地和 Travis CI 中有效,所以我不确定问题是什么。

标签: gitmacosgithubcontinuous-integrationgithub-actions

解决方案


user.name不是用户名,user.password也不存在。从文档user.name

请注意,这些变量的名称形式通常指的是某种形式的个人姓名。有关这些设置和选项的更多信息,请参阅 git-commit(1) 和 git(1) 的环境变量部分,credential.username如果您正在寻找身份验证凭据。

也就是说,user.name应该是您的(人)名,而不是用户名。

结果,系统会提示您输入用户名和密码,但您会失败,因为没有 TTY 可以提示您。在您的系统上,凭据可能已经保存在您的凭据助手中,这就是您没有看到提示的原因。

如果您想克隆另一个存储库,您应该首先创建一个个人访问令牌并将其存储在您的 CI 系统的秘密存储中(例如,作为名为 的环境变量TOKEN)。PAT 在您的密码发生变化时不会更改,可以独立于您的密码撤销和轮换,并且比大多数密码更安全。

然后,在您的 CI 工具中,运行以下命令:

$ git config credential.helper '!f(){echo username=token; echo "password=$TOKEN";};f'

这将设置一个凭证助手以使用TOKEN环境变量中的令牌作为您的密码。确保为您的克隆操作设置了环境变量,并且一切正常。


推荐阅读