每当我尝试推入我的存储库时,git都会要求用户名和密码。

每次重新输入密码没有问题,但问题是输入用户名。我使用https克隆存储库。

那么,我如何配置git,使它在每次git推送时都不需要用户名。

我是linux新手,但windows git push中的IIRC只要求输入密码。


当前回答

解决这个问题的正确方法是将http更改为ssh。

您可以使用此git remote rm源来删除远程回购。

然后,您可以使用ssh-sintax(可以从github复制)再次添加它:git远程添加原点git@github.com:username/reportname.git。

检查这篇文章。https://www.freecodecamp.org/news/how-to-fix-git-always-asking-for-user-credentials/

其他回答

git config --global credential.helper cache

编辑(由@dk14根据主持人和评论的建议)

警告:如果您从答案中使用credential.helper存储,您的密码将完全未加密(“原样”)存储在~/.git凭据中。请参阅下面的评论部分或“链接”部分的答案,特别是如果您的雇主对安全问题零容忍的话。

即使被接受,它也不能回答实际OP关于只省略用户名(而不是密码)的问题。对于有这个确切问题的读者,@grawity的答案可能会派上用场。


原始答案(作者@Alexander Zhu):

可以使用以下命令存储凭据

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

此外,我建议您阅读$git帮助凭据

截至8月13日,2021不再接受基于密码的登录。现在需要使用基于令牌/SSH的身份验证。

如果你不想每次推送时都被问到,请安装GitHubCLI,然后缓存凭据:

git config --global credential.helper store
gh auth login   

并按照提示进行操作。但请确保使用GitHub凭据检查Authenticate Git?带有Y。

我找到的最简单的方法是使用以下命令:

git config --global credential.https://github.com.username <your_username>

这会逐个站点工作,并修改全局git配置。

要查看更改,请使用:

git config --global --edit

使用Git存储库永久验证

运行以下命令以启用凭据缓存:

$ git config credential.helper store
$ git push https://github.com/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

使用还应指定缓存过期

git config --global credential.helper "cache --timeout 7200"

启用凭据缓存后,将缓存7200秒(2小时)。


读取凭证文档

$ git help credentials