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

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

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

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


当前回答

当我只使用git pull,git pull-origin xxx时,git会同时要求用户名和密码。

git config credential.helper store

它起作用。

其他回答

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

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

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

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

$ cat  ~/.git-credentials

ssh+密钥身份验证比https://credential.helper更可靠

您可以配置为对所有存储库使用SSH而不是HTTPS,如下所示:

git config --global url.ssh://git@github.com/.insteadOf https://github.com/

网址<此处记录了base>.installOf。

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

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

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

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

更改克隆的存储库可以:

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

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