如何解决GitHub在git推送时一直要求提供用户名密码的问题

1,220 阅读1分钟

git clone 我的一个现有的repo(在Github中添加了SSH密钥),修改了一些文件,并试图 ,但它一直要求提供用户名和密码,以进行 操作?git push git push

终端


git push

Username for 'https://github.com':
Password for 'https://github.com':  

GitHub 认证成功

终端


ssh -T git@github.com           

Hi mkyong! You've successfully authenticated, but GitHub does not provide shell access.

1.密码认证被删除

即使我们输入了正确的用户名和密码,Github仍然不允许推送,因为密码认证在2021年8月13日被移除。请使用个人访问令牌代替

终端


% git push     

Username for 'https://github.com': username@gmail.com
Password for 'https://username@gmail.com@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: Authentication failed for 'https://github.com/mkyong/spring-boot/'

2.使用SSH,而不是HTTPS

这个问题的主要原因是错误地使用了git clone HTTPS URL;我们需要SSH URL来使用SSH密钥。解决方法是更新.git/config 文件,url 值为SSH而不是HTTPS。

2.1 进入 repo 目录,打开文件.git/config.

.git/config


[core]
      repositoryformatversion = 0
      filemode = true
      bare = false
      logallrefupdates = true
      ignorecase = true
      precomposeunicode = true
[remote "origin"]
      url = https://github.com/mkyong/spring-boot
      fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
      remote = origin
      merge = refs/heads/master

2.2 将url 从HTTPShttps://github.com/mkyong/spring-boot 更新为SSHgit@github.com:mkyong/spring-boot.git

终端


[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@github.com:mkyong/spring-boot.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

完成后,再试试git push

参考资料