我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
当前回答
将用户名和密码存储在.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钩子中推送该规则来触发它。
堆栈溢出就是一个例子。
其他回答
您的问题:
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,而每次都不需要在提示中输入用户名和密码。那么我如何在Git中保存我的凭据?
如果您是github或其他提供商,我建议不要将它们保存在Git中,例如~/.Git凭据,而是将它们视为加密的机密。
将凭据设置为以下格式:
https://your_user_name:your_token@github.com/your_user_name/your_repo_name.git
将其作为加密机密,如secrets.REPOSTORY:
然后,您可以使用它来克隆公共或私有回购及其子模块,以及自动执行推拉操作
# put the credentials in a variable
export REPOSITORY=${{ secrets.REPOSITORY }}
# git clone public or private repos
git clone --recurse-submodules -j8 ${REPOSITORY}
# git pull will do automatic
cd /path/to/the/repo
git pull
# git push to a branch in the repo
git add . && \
git commit -m "Action from ${GITHUB_SHA}" && \
git push --force $REPOSITORY master:$BRANCH
在完整阅读了答案并尝试了这个问题的大部分答案之后,我终于找到了适合我的过程。我想分享它,以防有人不得不处理一个复杂的用例,但仍然不想像我一样浏览所有答案和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帮助。
您可以编辑~/.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 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将凭据存储在磁盘上。