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

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

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

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


当前回答

设置全天(即24小时)的凭据。

git config --global credential.helper 'cache --timeout=86400'

否则1小时,将86400秒替换为3600秒。

OR

“缓存”身份验证助手的所有配置选项:

git帮助凭据缓存

有关详细信息:https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

其他回答

设置全天(即24小时)的凭据。

git config --global credential.helper 'cache --timeout=86400'

否则1小时,将86400秒替换为3600秒。

OR

“缓存”身份验证助手的所有配置选项:

git帮助凭据缓存

有关详细信息:https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

除了user701648的答案之外,您还可以使用以下命令将凭据存储在主文件夹(所有项目的全局文件夹)中,而不是项目文件夹中

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

解决这个问题的正确方法是将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/

编辑(由@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帮助凭据

git config --global credential.helper cache