申请及使用 Token 以解决 Github 不再支持密码验证

内容纲要

查看【Git】专题可浏览更多内容

2020 年 7 月,Github 宣布打算要求对所有经过身份验证的 Git 操作使用基于令牌的身份验证(例如,个人访问、OAuth 或 GitHub 应用程序安装令牌)。从 2021 年 8 月 13 日开始,在 GitHub.com 上对 Git 操作进行身份验证时,将不再接受帐户密码。

也就是说,从 2021 年 8 月 13 日起,你对于 Github 如仓库的操作,可能会遇到这样的提示:

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.

解决方法也很简单,进入 Github 上的「Settings」里左侧列表底部的「Developer settings」,选择「Personal access tokens」并创建一个新的 token 就可以了。

附上地址:https://github.com/settings/tokens

注意保管好 token,它只会在页面上显示一次,如果弄丢了就只能再申请一次了。

在本地仓库操作时如果没有弹出要求输入 Github 用户名及 Token 的提示,可以使用命令:

$ git config --system --unset credential.helper

但每次操作都输入 Github 用户名及 Token 是挺麻烦的,所以可以使用 gi remote 命令设置远程仓库:

$ git remote set-url origin https://<token>@github.com/<Github 用户名>/<仓库名>.git

这样就不用每次对于远程仓库的操作都重复输入 Github 用户名及 Token 了。