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

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


当前回答

注意:此方法以明文形式将凭据保存在电脑磁盘上。计算机上的每个人都可以访问它,例如恶意NPM模块。

Run

git config --global credential.helper store

then

git pull

提供用户名和密码,这些详细信息稍后将被记住。凭证存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍为明文。

如果以后要更改密码

git pull

将失败,因为密码不正确,git然后从~/.git凭据文件中删除有问题的用户+密码,因此现在重新运行

git pull

以提供一个新密码,以便与之前一样工作。

其他回答

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

检查

git config --list

在完整阅读了答案并尝试了这个问题的大部分答案之后,我终于找到了适合我的过程。我想分享它,以防有人不得不处理一个复杂的用例,但仍然不想像我一样浏览所有答案和gitcredentials、gitcredential store等手册页。

如果你(像我一样)必须使用几个不同的用户名/密码组合来处理来自多个提供商(GitLab、GitHub、Bitbucket等)的多个存储库,请在下面找到我建议的步骤。如果您只有一个帐户可以使用,那么最好使用git-config--globalcredential.helperstore或git-config--globaluser.name“您的用户名”等解决方案,这些解决方案在前面的回答中已经得到了很好的解释。

我的解决方案:

取消设置全局凭据帮助器,以防以前的一些实验阻碍:)git-config--全局--取消设置凭据s.helper移动到repo的根目录并禁用本地凭据助手(如果需要)cd/path/to/my/repogit-config--取消设置凭据帮助程序`创建一个文件以将回购凭证存储到git-config credential.helper“存储--文件~/.git_repo_credentials”注意:此命令在您的主目录中创建一个名为“.git_repo_credentials”的新文件,git将您的凭据存储到该文件中。如果不指定文件名,Git将使用默认的“.Git_credentials”。在这种情况下,只需发出以下命令即可:git-config credential.helper存储设置您的用户名git配置凭据。*.username my_user_name注意:如果您的存储库来自同一个提供商(例如GitLab),通常可以使用“*”。如果您的存储库由不同的提供程序托管,那么我建议为每个存储库显式设置到提供程序的链接,如下面的示例(对于GitLab):git配置凭据。https://gitlab.com.username我的用户名(_U)

此时,如果发出需要凭据的命令(例如git pull),将要求您输入与“my_user_name”对应的密码。这只需要一次,因为git将凭据存储到“.git_repo_credentials”中,并在后续访问时自动使用相同的数据。

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

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

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