我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。

那么我如何在Git中保存我的凭据?


当前回答

打开凭据助手,以便Git将您的密码保存在内存中一段时间:

在终端中,输入以下内容:

# Set Git to use the credential memory cache
git config --global credential.helper cache

默认情况下,Git将缓存您的密码15分钟。

要更改默认密码缓存超时,请输入以下内容:

# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

来自GitHub帮助。

其他回答

对于有相同问题的gitlab用户-

您可以设置一个部署令牌来克隆或从存储库中提取(不能使用部署令牌将代码推送到repo)。

在这里,您可以了解有关gitlab部署令牌的更多信息:https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html

创建部署令牌后,请使用以下方法克隆回购:

git clone https://${username}:${deploy_token}@gitlab.com/yourusername/yourreponame.git

我认为这种方法比全局保存git用户名和git密码更好(例如,在远程共享计算机中可能不安全)

除了编辑~/.gitconfig文件外,如果从命令行调用,还可以执行以下操作:

git config --local --edit

or

git config --global --edit

在默认文本编辑器中编辑git配置文件

您还可以使用命令行直接编辑git配置文件(无需编辑器)

git config --local user.name 'your username'
git config --local user.password 'your password'

or

git config --global user.name 'your username'
git config --global user.password 'your password'

请注意,始终使用单引号。如果您使用双引号,您的用户名和密码可能会使用一些会破坏密码的字符。

--local或--global表示为项目或OS用户保存配置参数。

注意:此方法以明文形式将凭据保存在电脑磁盘上。计算机上的每个人都可以访问它,例如恶意NPM模块。

Run

git config --global credential.helper store

then

git pull

提供用户名和密码,这些详细信息稍后将被记住。凭证存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍为明文。

如果以后要更改密码

git pull

将失败,因为密码不正确,git然后从~/.git凭据文件中删除有问题的用户+密码,因此现在重新运行

git pull

以提供一个新密码,以便与之前一样工作。

如果您正在Windows上使用Git凭据管理器。。。

git-config-l应该显示:

credential.helper=manager

但是,如果没有提示您输入凭据,请执行以下步骤:

从“开始”菜单打开“控制面板”选择用户帐户在左侧菜单中选择管理凭据删除与Git或GitHub相关的任何凭据

如果您有代理并且Git服务器位于内部网络上,请确保您没有设置HTTP_PROXY、HTTPS_PROXY、NO_PROXY环境变量。

您还可以使用gitgui测试Git fetch/push/pull,gitgui链接到C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\Git-core中的凭据管理器二进制文件

您可以使用git凭据存储将未加密的密码存储在磁盘上,仅受文件系统权限的保护。

实例

git config credential.helper store
git push http://example.com/repo.git

Username: <type your username>
Password: <type your password>

[Several days later]

git push http://example.com/repo.git

[Your credentials are used automatically]

您可以检查存储在文件~/.git凭据中的凭据。

有关更多信息,请访问git credential store-Helper将凭据存储在磁盘上。