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

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

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

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


当前回答

我使用将我的Git存储库用户名和密码存储在磁盘上。所以每次我推送代码时,git都不会询问用户名和密码

按照以下命令在本地存储中保存凭据

$ git config credential.helper store
or
$ git config --global credential.helper store

从现在起,Git将在第一次访问每个URL上下文时将凭据写入~/.Git凭据文件。要查看此文件的内容,可以使用cat命令,如图所示。

$ cat  ~/.git-credentials

其他回答

您可以在本地存储库的.git/config文件中完成此操作。此文件包含一个名为“remote”的节,其中包含名为“url”的条目。“url”条目应包含您正在讨论的存储库的https链接。

当您在主机“url”前面加上用户名时,git不应该再要求您输入用户名。下面是一个示例:

url = https://username@repository-url.com

使用以下命令可以给您15分钟的超时时间。

$ git config --global credential.helper cache

您也可以通过下面的命令修改时间。这是一个1小时的示例。

$ git config --global credential.helper 'cache --timeout=3600'

如果要在本地缓存,请使用该命令一小时以超时。

$ git config credential.helper 'cache --timeout=3600'

使用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

确保您使用的是SSH而不是http。检查此SO答案。

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

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

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

要查看更改,请使用:

git config --global --edit