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

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


当前回答

您可以使用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凭据中

.git credentials是运行git-config--globalcredential.helper store时存储用户名和密码(访问令牌)的位置,这是其他答案所建议的,然后键入用户名和密码或访问令牌:

https://${username_or_access_token}:${password_or_access_token}@github.com

因此,为了保存用户名和密码(访问令牌):

git config —-global credential.helper store
echo “https://${username}:${password_or_access_token}@github.com“ > ~/.git-credentials

这对于GitHub机器人非常有用,例如,通过为不同分支设置规则来解决同一Docker Hub存储库中的Chain自动化构建,然后通过在Docker Hub中的post_push钩子中推送该规则来触发它。

堆栈溢出就是一个例子。

对于有相同问题的gitlab用户-

您可以设置一个部署令牌来克隆或从存储库中提取(不能使用部署令牌将代码推送到repo)。

在这里,您可以了解有关gitlab部署令牌的更多信息:https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html

创建部署令牌后,请使用以下方法克隆回购:

git clone https://${username}:${deploy_token}@gitlab.com/yourusername/yourreponame.git

我认为这种方法比全局保存git用户名和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

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

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