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

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


当前回答

双因素身份验证改变了用户对网站的身份验证方式,但Git仍然假设用户可以从内存中键入密码。

介绍git credential oauth:一个git凭据助手,可以使用oauth安全认证GitHub、GitLab、BitBucket和其他伪造文件。

没有更多密码!不再有个人访问令牌!不再有SSH密钥!第一次推送时,助手将打开浏览器窗口进行身份验证。缓存超时内的后续推送不需要交互。

从安装https://github.com/hickford/git-credential-oauth/releases/

配置方式:

git config --global --unset-all credential.helper
git config --global --add credential.helper "cache --timeout 7200" # two hours
git config --global --add credential.helper oauth

其他回答

您只需修改~/.git凭据

然后添加以下行:

git:https://<user>:<token/password>@gitlab.com

就是这样

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

Run

git config --global credential.helper store

then

git pull

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

如果以后要更改密码

git pull

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

git pull

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

除了编辑~/.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用户保存配置参数。

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

检查

git config --list

我认为缓存凭据比永久存储更安全:

git config --global credential.helper 'cache --timeout=10800'

现在,您可以输入用户名和密码(git pull或…),并在接下来的三个小时内继续使用git。

它很好,很安全。

超时的单位是秒(在本例中为三小时)。