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

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

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

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


当前回答

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

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

其他回答

快速方法

在工作目录中获取git的url:

git config remote.origin.url

那么:

git config credential.helper store

git push "url of your git"

将最后一次询问用户名和密码

完成。

更改克隆的存储库可以:

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

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

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

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

编辑(由@dk14根据主持人和评论的建议)

警告:如果您从答案中使用credential.helper存储,您的密码将完全未加密(“原样”)存储在~/.git凭据中。请参阅下面的评论部分或“链接”部分的答案,特别是如果您的雇主对安全问题零容忍的话。

即使被接受,它也不能回答实际OP关于只省略用户名(而不是密码)的问题。对于有这个确切问题的读者,@grawity的答案可能会派上用场。


原始答案(作者@Alexander Zhu):

可以使用以下命令存储凭据

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

此外,我建议您阅读$git帮助凭据

我尝试了这些步骤,结果成功了:

创建个性化访问令牌:https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token将令牌添加到Android studio(文件->设置->版本控制->Github->添加-使用令牌登录)转到文件->设置->版本控制->Git并取消选中“使用凭据助手”