背景 : 去年在往 github 远程仓库 push 代码时 遇到了如下的错误: fatal: unable to access 'github.com/chesterzhan… ': Failed to connect to github.com port 443: Timed out
多次尝试后也会出现下面这种报错 Logon failed, use ctrl+c to cancel basic credential prompt. remote: Support for password authentication was removed on August 13, 2021. remote: Please see docs.github.com/en/get-star… forformation on currently recommended modes of authentication. fatal: Authentication failed for 'github.com/chesterzhan…'
命令行会弹出一个 github 登录的窗口,让你登录 github
但问题是,无论你如何登录,始终提示密码错误。无论用 github profile 里面的名字,还是用邮箱登录,都是如此。 当时花费了我三个小时解决,今天又遇到了这个巨坑,下定决心记录下来,希望帮助大家避坑。
原因分析:
命令行弹出来的 Github 登录窗口,并不是让你登录 github , 而是让你在 github 中生成一个 token,用 token 登录。 你用 GitHub 账号或者邮箱登录是永远登录不上去的。
解决方法:
在 github 中生成一个新的 token。
- 点击个人头像,进入setteing, 再点击左下角的 Developer settings
- 点击 Personal access tokens, 再点击右上角的 Generate new token
- 设置 username 和权限
在红色圈圈那里的 Note 输入 username,git 命令行弹出来的 github 窗口的 username就来源于这里
Expiration 那里可以选择 token 过期时间,select scopes 那里勾选权限。我一般设置为永久有效和全部权限。拉到最下面 点击生成 token。
然后既可以看到新生成的token了,这里要把 token 记录下来,因为这个token 以后不会再次显示了。 拿着这个token和 生成token时在 note 框框里输入的username 就可以在本地登录 github 了。