这是我参与8月更文挑战的第16天,活动详情查看:8月更文挑战
问题背景
小背景
从GitHub拉取代码以及向GitHub推送代码时,出错了。错误信息如下:
$ git fetch
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': username
Password for 'https://username@github.com':
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/liupjie/mybatis.git/': The requested URL returned error: 403
大背景
也就是GitHub处于账户安全的考虑,所以去年(2020-07)宣布,打算通过令牌的方式来进行Git操作的验证。并且从2021年8月13日开始不再接受账户密码的验证方式。所以就出现了如上的错误信息。
- 受影响的工作流
- Git命令行
- 使用Git的桌面应用(GitHub Desktop不受影响)
- 任何直接使用密码访问GitHub仓库的应用或服务
问题解决
一、获取personal access token
1. 登录GitHub
2. 点击右上角头像,进入Settings
3. Developer settings
4. Personal access tokens
5. Generate new token
- 填写表格
- Note是备注,会在
Personal access tokens列表中展示 - 过期时间随便选(默认30天),图省事就选
No expiration - 权限我只选择了跟仓库repo相关的内容,其他的看个人情况,随意选
- 完成之后点击表格下方的
Generate Token即可
6. 生成的personal access token令牌如下
确保将令牌拷贝下来,否则就看不到了。只能重新创建一个。
7. 刷新页面之后,令牌看不见了
- 刚才表格中填写的Note备注,会在这里展示
二、在本地机器中的设置令牌
Windows系统
1. 打开控制面板,找到凭据管理器并打开
2. 选择Windows凭据,并找到git:https://github.com,点击展开,然后点击编辑按钮
3. 将密码替换为令牌即可
基于Linux的操作
如果是基于命令行的Git操作,或者是在Linux系统中,使用如下方式。
1. 配置用户名,邮箱
$ git config --global user.name "your_github_username"
$ git config --global user.email "your_github_email"
$ git config -l
2. 进行git操作时(如:git fetch)会提示输入用户名密码,此时将令牌作为密码输入即可
3. 缓存令牌到本地
$ git config --global credential.helper cache
4. 删除本地的令牌缓存
$ git config --global --unset credential.helper
以上便是本次GitHub关于令牌校验更新的介绍。