太长不看:你以前怎么登录,现在就怎么登录,你以前怎么改密码,现在就怎么改,只不过要把用户名换成oauth2
,密码换成你的令牌。
今年 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. fatal: unable to access 'https://github.com/<user>/<repo>/': The requested URL returned error: 403
解决方案就是使用访问令牌(AccessToken)。如果Git 客户端提示你需要登录 Github,账户输入oauth2,密码输入你的令牌。
如果你已经用账户密码登录过了。在推送的时候,不会提示你登录,而是直接报错。这种情况就需要打开~/.git-credentials文件,这个是Git客户端储存账号密码的地方。找到@github.com,把前面的用户名密码替换成oauth2:<token>,即大功告成。
注:~代表用户目录,在 Windows 下,它是C:\Users\<username>\,在 Linux 下,它是/home/<username>/。不过不需要关心这个,因为 Bash 或者 PowerShell 会自动识别它。
注意,如果你的用户名密码在你仓库的远程 URL 里面,同样需要用git remote set-url来替换掉。
其实所有 Git 服务端都可以这样操作,这样支持权限隔离和细粒度控制,避免一码通密码,更加安全。