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

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

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

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


当前回答

在GitHub上添加新的SSH密钥,如本文所述。

如果Git仍然要求您输入用户名和密码,请尝试更改https://github.com/到git@github.com:在远程URL中:

$ git config remote.origin.url 
https://github.com/dir/repo.git

$ git config remote.origin.url "git@github.com:dir/repo.git"

其他回答

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

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

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

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

当一个人使用HTTPS而不是SSH下载时,就会出现这种情况。我实现的最简单的方式是,当我做了一些更改时,我推送了所有内容,其中要求输入用户名和密码,然后我从我的机器中删除了目录,并将其从gitclone SSH地址中删除。解决了。

只需使用SSH而不是HTTP克隆它,它就不会要求用户名或密码。

另外,即使使用SSH下载,使用双因素身份验证也会产生问题,因此禁用它可以解决问题。

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

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

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

要查看更改,请使用:

git config --global --edit

在GitHub上添加新的SSH密钥,如本文所述。

如果Git仍然要求您输入用户名和密码,请尝试更改https://github.com/到git@github.com:在远程URL中:

$ git config remote.origin.url 
https://github.com/dir/repo.git

$ git config remote.origin.url "git@github.com:dir/repo.git"