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

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

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

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


当前回答

如果您使用的是https而不是ssh,您可以按照user701648所说的那样在.git/config中编辑“url”,但如果您愿意,也可以添加密码:

url = https://username:password@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'

截至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

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

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