我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
当前回答
只需将您的凭据放入URL,如下所示:
https://Username`**:**`Password`**@**`github.com/myRepoDir/myRepo.git`
您可以这样存储:
git remote add myrepo https://Userna...
…使用它的示例:
git push myrepo master`
现在就是列出URL别名:
git remote -v
…以及删除其中一个的命令:
git remote rm myrepo
其他回答
将用户名和密码存储在.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凭据存储将未加密的密码存储在磁盘上,仅受文件系统权限的保护。
实例
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将凭据存储在磁盘上。
根据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内置和自制版本都有它。
注意:此方法以明文形式将凭据保存在电脑磁盘上。计算机上的每个人都可以访问它,例如恶意NPM模块。
Run
git config --global credential.helper store
then
git pull
提供用户名和密码,这些详细信息稍后将被记住。凭证存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍为明文。
如果以后要更改密码
git pull
将失败,因为密码不正确,git然后从~/.git凭据文件中删除有问题的用户+密码,因此现在重新运行
git pull
以提供一个新密码,以便与之前一样工作。
如果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是您可以在设置/开发人员设置/个人访问令牌中生成的内容。记住授予它回购范围。