我创建了两个 ssh 密钥,第一个供我个人使用, 我刚刚为工作/学校创建的第二个。
当我使用 SSH 和第二个 SSH 密钥登录使用 git CLI 时,它说 我已经成功登录,如下所示
$ winpty gh auth login
? What account do you want to log into? GitHub.com
? You're already logged into github.com. Do you want to re-authenticate? Yes
? What is your preferred protocol for Git operations? SSH
? Upload your SSH public key to your GitHub account? C:\Users\dykim\.ssh\id_rsa.
pub
? Title for your SSH key: (GitHub CLI) second-key
? Title for your SSH key: future_proof
? How would you like to authenticate GitHub CLI? Login with a web browser
! First copy your one-time code: 0B1D-6370
Press Enter to open github.com in your browser...
✓ Authentication complete.
- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
HTTP 422: Validation Failed (https://api.github.com/user/keys)
key is already in use
所以不知何故,身份验证没有问题。 但我担心这会在以后以某种方式突然出现并成为一个问题 致力于我未来的工作项目。
我该怎么做才能避免这个问题?
这似乎类似于待报告的问题 #5299:“[当 SSH 密钥已在帐户上注册时出现虚假身份验证错误][1]”
重现行为的步骤
- 运行
并选择已在帐户中注册的 SSH 密钥gh auth login
- 即使密钥可以正常工作,也会出现上述错误
预期行为与实际行为
我认为有几种方法可以改进已注册密钥的处理:
- 鉴于 CLI 应用程序被授予对用户注册的 SSH 密钥的完全权限,它可能会首先列出已注册的密钥,并且仅在尚未注册的情况下尝试注册新密钥。
那么只有当密钥在不同的帐户上注册时才会出现错误(或者您在尝试注册 CLI 应用程序的同时通过网络浏览器上传密钥,这将是一件奇怪的事情)- 自定义此步骤失败时的错误消息,如“密钥已在使用中(注意:如果此 SSH 密钥已在您的帐户上注册,则预计会出现此错误)”
2024 年第 2 季度:自 gh 2.48.0 起,您可以使用新的
--skip-ssh-key
选项无需选择即可登录:
gh auth login -h='GitHub.com' -p='SSH' --skip-ssh-key -w
请参阅问题 8508 和 PR 8935。 [1]:https://github.com/cli/cli/issues/5299