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

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


当前回答

打开凭据助手,以便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凭据存储将未加密的密码存储在磁盘上,仅受文件系统权限的保护。

实例

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将凭据存储在磁盘上。

您可以编辑~/.gitconfig文件以存储凭据:

sudo nano ~/.gitconfig

应该已经有了

[user]
        email = your@email.com
        user = gitUSER

您应该在该文件的底部添加以下内容。

[credential]
        helper = store

我推荐这个选项的原因是因为它是全局的,如果在任何时候你需要删除这个选项,你就知道该去哪里更改它。

仅在个人计算机中使用此选项。

然后,当您拉|clone|输入Git密码时,通常,密码将以以下格式保存在~/.Git凭据中

https://gituser:gitpassword@domain.xxx

其中DOMAIN.XXX可以是github.com、bitbucket.org或其他

请参阅文档。

重新启动终端。

将用户名和密码存储在.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钩子中推送该规则来触发它。

堆栈溢出就是一个例子。

只需使用

git config --global credential.helper store

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

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

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

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

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

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

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