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

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


当前回答

在浏览了数十篇StackOverflow文章、博客等之后,我尝试了每一种方法,这就是我想到的。它涵盖了一切。

普通的DevOps Git证书和私有包备忘单

这些都是在没有交互式密码提示的情况下安全验证Git以克隆存储库的所有方法和工具。

SSH公钥SSH-ASKPASSAPI访问令牌GIT_ASKPASS.gitconfig代替.gitconfig[凭据].git凭据.netrc文件私人套餐(免费)Node.js/npm包.jsonPython/pip/geggs requirements.txt红宝石宝石宝石文件转到.mod

银子弹

只想工作™? 这是神奇的银弹。

获取您的访问令牌(如果您需要GitHub或Gitea说明,请参阅备忘单中的部分),并将其设置在环境变量中(用于本地开发和部署):

MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

对于GitHub,逐字复制并运行以下行:

git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"

祝贺现在,无论是使用HTTPS还是SSH URL,任何自动克隆Git存储库的工具都不会受到密码提示的阻碍。

不使用GitHub?

对于其他平台(Gitea、GitHub和Bitbucket),只需更改URL即可。不要更改用户名(尽管是任意的,但不同的配置条目需要它们)。

兼容性

这在macOS、Linux、Windows(Bash)、Docker、CircleCI、Heroku、Akkeris等本地运行。

更多信息

请参阅备忘单的“.gitconfig insteadOf”部分。

安全

请参阅备忘单的“安全”部分。

其他回答

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

检查

git config --list

对于Windows用户,请查看.gitconfig文件并检查为凭据助手配置了什么。如果您有以下内容。。。

[凭证“helperselector”]selected=绞车

您将在Windows凭据管理器中找到凭据。

您可以在那里编辑凭证。

注意:Wincred已被弃用,请参阅。。。

https://github.com/Microsoft/Git-Credential-Manager-for-Windows#notice-该项目不再处于警告状态

因此,您可能需要重新配置Git以使用内置的Git凭据管理器。。。

git config --global credential.helper manager

双因素身份验证改变了用户对网站的身份验证方式,但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 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将凭据存储在磁盘上。

如果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是您可以在设置/开发人员设置/个人访问令牌中生成的内容。记住授予它回购范围。