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

那么我如何在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用户保存配置参数。

其他回答

根据rofrol在Linux Ubuntu上的评论,根据这个答案,下面是如何在Ubuntu上实现的:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

其他一些发行版提供二进制文件,因此您不必构建它。

在OSX中,它通常“内置”了一个默认模块“osxkeychain”,因此您可以免费获得它。默认情况下,OS X内置和自制版本都有它。

您可以使用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 config --global user.name "your username"
git config --global user.password "your password"

检查

git config --list

您可以使用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密钥。

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

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

ssh-add -K ~/.ssh/id_rsa

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