我想在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

其他回答

截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!

安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码浏览器内OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。

推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。

外观配置(文档):

首选在终端而不是GUI中选择身份验证方法(点击次数较少)始终使用浏览器方法,而不是每次都被询问(甚至更少的按键)

git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser

对于全局设置,打开终端(从任何位置),运行以下命令:

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

这样,您计算机上的任何本地Git存储库都将使用该信息。

您可以通过执行以下操作为每个存储库单独配置:

在存储库文件夹中打开终端。运行以下操作:git-config user.name“您的用户名”git-config user.password“您的密码”

它只影响该文件夹(因为您的配置是本地的)。

如果git客户端不关心安全性,请按以下方式编辑url:

git remote set-url origin https://${access_token}@github.com/${someone}/${somerepo}.git

在git clone案例中也是如此:

git clone https://${access_token}@github.com/${someone}/${somerepo}.git

我个人不喜欢全局域的git-config,因为在多个帐户的情况下,这会很糟糕。

access_token是您可以在设置/开发人员设置/个人访问令牌中生成的内容。记住授予它回购范围。

只需使用

git config --global credential.helper store

然后做数字拉动。它将要求输入用户名和密码。从现在起,它将不会提供任何用户名和密码提示。它将存储详细信息。

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

检查

git config --list