Message “Support for password authentication was removed.”
技术背景
从2021年8月13日起,GitHub不再接受使用账户密码进行Git操作的身份验证,而是需要使用个人访问令牌(PAT)来替代。这是因为密码容易被意外泄露,而个人访问令牌可以进行权限限制,降低风险。
实现步骤
创建个人访问令牌
- 登录GitHub账户,进入Settings -> Developer Settings -> Personal Access Token -> Tokens (classic) -> Generate New Token。
- 输入密码,填写表单,点击Generate token。
- 复制生成的令牌,格式类似
ghp_sFhFsSHhTzMDreGRLjmks4Tzuzgthdvfsrta
。
不同操作系统配置PAT
Windows
- 从Control Panel进入Credential Manager -> Windows Credentials。
- 若找到
git:https://github.com
,点击Edit,将密码替换为GitHub个人访问令牌。 - 若未找到
git:https://github.com
,点击Add a generic credential,Internet地址填git:https://github.com
,用户名填GitHub用户名,密码填个人访问令牌,点击Ok。
macOS
- 点击菜单栏右侧的Spotlight图标(放大镜),输入Keychain access并回车打开应用。
- 在Keychain Access中搜索
github.com
。 - 找到
github.com
的internet password条目,相应地编辑或删除该条目。
Linux
- 配置本地GIT客户端的用户名和邮箱地址:
1 2 3
| git config --global user.name "your_github_username" git config --global user.email "your_github_email" git config -l
|
- 使用GIT访问GitHub,例如克隆仓库:
1
| git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
|
- 输入用户名和个人访问令牌。
- 缓存记录,让计算机记住令牌:
1
| git config --global credential.helper cache
|
- 若需要,可随时删除缓存记录:
1 2
| git config --global --unset credential.helper git config --system --unset credential.helper
|
- 验证:
JetBrains IDEs配置
以IntelliJ为例:
- 按下
⌘Cmd + 0
/Ctrl + Alt + 0
打开设置,选择Version Control | GitHub。 - 点击Add按钮。
- 选择Log In with Token。
- 在文本框中插入令牌。
- 点击Add Account。
核心代码
克隆仓库时使用PAT
1
| git clone https://<username>:<githubtoken>@github.com/<username>/<repositoryname>.git
|
修改远程仓库URL使用PAT
1
| git remote set-url origin https://<token>@github.com/<username>/<repo>.git
|
清除凭证管理器缓存
设置凭证管理器
1
| git config --global credential.helper manager
|
1
| git config --global credential.helper osxkeychain
|
1
| git config --global credential.helper libsecret
|
1
| git config --global credential.helper store
|
最佳实践
- 为个人访问令牌设置合理的过期时间和权限范围,提高安全性。
- 使用凭证管理器保存令牌,避免每次操作都手动输入。
- 定期更换个人访问令牌,降低泄露风险。
常见问题
找不到凭证管理器中的GitHub条目
可以尝试克隆一个项目,Git会提示输入用户名和密码,此时输入用户名和个人访问令牌,条目就会被创建。
Git反复要求输入用户名和令牌
- 运行
nano ~/.git-credentials
,删除GitHub相关行并保存。 - 执行
git config --global credential.helper store
,将令牌保存到文件中,但存在一定风险。 - 运行
git pull
,仅需输入一次用户名和密码。
macOS KeyChain Access中找不到github.com条目
- 尝试进行一次Git操作,系统会要求输入用户名。
- 输入GitHub用户名。
- 从Personal access tokens生成一个新密钥。
- 在密码字段中输入新生成的令牌值,此时可以在KeyChain Access -> login中看到新的github.com条目。