1. 报错
remote: Support for password authentication was removed on August 13, 2021.remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.fatal: Authentication failed for
2. 错误解释
GitHub 自 2021 年 8 月 13 日起不再支持密码认证,你需要采用其他认证方式,比如使用个人访问令牌(Personal Access Token)或者 SSH 密钥。
3. 解决办法
- 使用个人访问令牌(Personal Access Token);
- 使用 SSH 密钥。
4. 使用个人访问令牌(Personal Access Token)
4.1 生成个人访问令牌
- 登录你的 GitHub 账号,点击右上角的头像,选择 “Settings”。
- 在左侧菜单中,点击 “Developer settings”。
- 选择 “Personal access tokens”,然后点击 “Generate new token”。
- 给令牌命名,选择需要的权限(一般克隆仓库选择 repo 权限即可),设置令牌的过期时间。
- 点击 “Generate token” 生成令牌,注意要及时保存这个令牌,因为页面刷新后就无法再次查看。
4.2 使用令牌进行认证
在命令行中,当你进行 git push 或者 git clone 操作时,将原来使用的密码替换为个人访问令牌。
例如,克隆仓库时:
git clone https://github.com/xxx/xxx.git
在输入密码的提示处,输入你刚才生成的个人访问令牌。
4.3 更新本地仓库的认证信息(可选)
如果你之前已经克隆了仓库,并且使用的是旧的认证方式,可以通过以下命令更新认证信息:
git remote set-url origin https://<your-github-username>:<your-personal-access-token>@github.com/xxx/xxx.git
将 your-github-username 替换为你的 GitHub 用户名, your-personal-access-token 替换为你生成的个人访问令牌。
5. 使用 SSH 密钥
5.1 生成 SSH 密钥
打开终端,输入以下命令生成 SSH 密钥:
ssh-keygen -t ed25519 -C "your_email@example.com"
将 your_email@example.com 替换为你在 GitHub 上注册的邮箱地址。按照提示完成密钥生成过程,生成的密钥默认存储在 ~/.ssh/id_ed25519 和 ~/.ssh/id_ed25519.pub 。
5.2 添加 SSH 密钥到 GitHub
- 复制公钥内容:
cat ~/.ssh/id_ed25519.pub
- 登录 GitHub,点击右上角的头像,选择 “Settings”。
- 在左侧菜单中,点击 “SSH and GPG keys”。
- 点击 “New SSH key”,将复制的公钥内容粘贴到 “Key” 字段,给密钥命名,然后点击 “Add SSH key”。
5.3 使用 SSH 地址克隆仓库
- 将原来使用的 HTTPS 地址替换为 SSH 地址进行克隆:
git clone git@github.com:xxx/xxx.git
- 删除原来的远程库,添加新的 SSH 地址:
git remote remove origin
git remote add origin git@github.com:xxx/xxx.git
git push -u origin master -f