我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
当前回答
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”中,并在后续访问时自动使用相同的数据。
除了编辑~/.gitconfig文件外,如果从命令行调用,还可以执行以下操作:
git config --local --edit
or
git config --global --edit
在默认文本编辑器中编辑git配置文件
您还可以使用命令行直接编辑git配置文件(无需编辑器)
git config --local user.name 'your username'
git config --local user.password 'your password'
or
git config --global user.name 'your username'
git config --global user.password 'your password'
请注意,始终使用单引号。如果您使用双引号,您的用户名和密码可能会使用一些会破坏密码的字符。
--local或--global表示为项目或OS用户保存配置参数。
要将用户名和用户密码保存到github帐户中,只需按顺序运行这些命令。
git config --global user.name "userName"
git config --global user.email "usernam@gmail.com"
git config --global user.password "userPassword"
git config --global credential.helper store
git config --list --show-origin
然后使用以下命令生成密钥:
ssh-keygen -t rsa -C "useremail@gmail.com"
注意:复制创建id_rsa文件的文件位置然后转到该文件位置-->打开gitbash或命令提示符-->运行命令-cat id_rsa.pub
将显示SSH密钥,复制此SSH密钥并将其粘贴到gihub/gitlab帐户中
双因素身份验证改变了用户对网站的身份验证方式,但Git仍然假设用户可以从内存中键入密码。
介绍git credential oauth:一个git凭据助手,可以使用oauth安全认证GitHub、GitLab、BitBucket和其他伪造文件。
没有更多密码!不再有个人访问令牌!不再有SSH密钥!第一次推送时,助手将打开浏览器窗口进行身份验证。缓存超时内的后续推送不需要交互。
从安装https://github.com/hickford/git-credential-oauth/releases/
配置方式:
git config --global --unset-all credential.helper
git config --global --add credential.helper "cache --timeout 7200" # two hours
git config --global --add credential.helper oauth
在这种情况下,您需要git凭据助手通过以下命令行告诉git记住您的GitHub密码和用户名:
git config --global credential.helper wincred
如果您使用的是使用SSH密钥的存储库,则需要SSH密钥进行身份验证。