当前位置: 首页 > 工具软件 > git-remote-hg > 使用案例 >

git push 需要输入用户名和密码,原因分析及解决

呼延俊良
2023-12-01

参考:
https://help.github.com/en/articles/which-remote-url-should-i-use
https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password
https://help.github.com/en/articles/caching-your-github-password-in-git
https://help.github.com/en/articles/securing-your-account-with-two-factor-authentication-2fa

当从网上克隆一个仓库时,有两种URL可以选择,一种是HTTPS URLs(任何场景下都可以工作,推荐方式),另一种是SSH URLs。

通常我们选择的是使用HTTPS URL来clone仓库
当使用HTTPS方式克隆仓库时,对仓库的一些更改操作,比如说git push,需要用户输入username and password。这是因为Github采取了two-factor authentication,双重验证方式来保证安全。(每次验证的时候,需要输入用户名和密码),或者如果要授权某个组织,给第三方软件,来访问Github,则需要提供personal access token

如果不想每次输入用户名和密码,则可以使用credential.helper(凭证助手)来记住Username和Password。

$ git config credential.helper store
$ git push https://github.com/owner/repo.git
# 然后输入用户名和密码
Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

这样下次再git push时,就不用输入用户和密码了。
还可以设置记住时间:

git config --global credential.helper 'cache --timeout 7200'  #  这里设置账号信息被记住7200秒,两个小时。
 类似资料: