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

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

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

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


当前回答

您可以通过在Git配置文件中放置以下内容来设置给定站点上所有存储库的用户名。你会想改变的”https://example.com“和”我“添加到实际URL和您的实际用户名。

[credential "https://example.com"]
    username = me

(这直接来自“git help credentials”)

其他回答

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

更改克隆的存储库可以:

git remote set-url origin git@github.com:gitusername/projectname.git

使用缓存的凭据是有效的,设置超时时间可以减少麻烦。如果您使用Windows凭据助手,这应该是最简单的方法(特别是在SSH被阻止的情况下)。

最简单的方法是创建包含以下内容的~/.netrc文件:

machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_PASSWORD

(如下所示:https://gist.github.com/ahoward/2885020)

您甚至可以关闭此文件的权限,以便没有人可以通过键入以下内容读取您的密码:

chmod 600 ~/.netrc

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

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

$ git config --global credential.helper cache

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

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

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

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