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

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


当前回答

全局保存用户名和密码:

git config --global user.name "fname lname"
git config --global user.email "example@gmail.com"
git config --global user.password "secret"

获取特定设置,

git config --global --get user.name
git config --global --get user.email
git config --global --get user.password

正在获取所有Git设置:

git config --list --show-origin

其他回答

如果您使用SSH身份验证而不是用户名/密码身份验证,您将更加安全。

如果您使用的是Mac,SSH客户端身份验证将集成到macOS密钥链中。创建SSH密钥后,在终端中键入:

ssh-add -K ~/.ssh/id_rsa

这将向macOS密钥链添加SSH私钥。Git客户端连接到远程服务器时将使用SSH。只要您在服务器上注册了ssh公钥,就可以了。

打开凭据助手,以便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帮助。

GitHub的建议现在已经改变,最好的方法也是最简单的。此处显示详细信息

安装Github官方cli。例如,对于mac:brew install gh。在终端中键入gh-auth-login,然后按照提示进行操作。

您可以使用gitconfig在git中启用凭据存储。

git config --global credential.helper store

运行此命令时,当您第一次从远程存储库中拉入或推送时,会询问您的用户名和密码。

之后,对于与远程存储库的后续通信,您不必提供用户名和密码。

存储格式为.git凭据文件,以明文形式存储。

此外,您还可以为git-configcredential.helper使用其他助手,即内存缓存:

git config credential.helper 'cache --timeout=<timeout>'

它采用可选的超时参数,确定凭证将在内存中保留多长时间。使用帮助器,凭据将永远不会接触磁盘,并且在指定的超时后将被擦除。默认值为900秒(15分钟)。


警告:如果使用此方法,您的Git帐户密码将以明文格式保存在global.gitconfig文件中,例如在Linux中,它将是/home/[用户名]/.gitconfig。

如果您不希望这样做,请为您的帐户使用ssh密钥。

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

检查

git config --list