我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
当前回答
对于有相同问题的gitlab用户-
您可以设置一个部署令牌来克隆或从存储库中提取(不能使用部署令牌将代码推送到repo)。
在这里,您可以了解有关gitlab部署令牌的更多信息:https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html
创建部署令牌后,请使用以下方法克隆回购:
git clone https://${username}:${deploy_token}@gitlab.com/yourusername/yourreponame.git
我认为这种方法比全局保存git用户名和git密码更好(例如,在远程共享计算机中可能不安全)
其他回答
要将用户名和用户密码保存到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 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
截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!
安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码浏览器内OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。
推送时,您可以选择身份验证方法:
> git push
Select an authentication method for 'https://github.com/':
1. Web browser (default)
2. Device code
3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...
在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"
熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。
外观配置(文档):
首选在终端而不是GUI中选择身份验证方法(点击次数较少)始终使用浏览器方法,而不是每次都被询问(甚至更少的按键)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
您可以使用gitconfig在git中启用凭据存储。
git config --global credential.helper store
运行此命令时,当您第一次从远程存储库中拉入或推送时,会询问您的用户名和密码。
之后,对于与远程存储库的后续通信,您不必提供用户名和密码。
存储格式为.git凭据文件,以明文形式存储。
此外,您还可以为git-configcredential.helper使用其他助手,即内存缓存:
git config credential.helper 'cache --timeout=<timeout>'
它采用可选的超时参数,确定凭证将在内存中保留多长时间。使用帮助器,凭据将永远不会接触磁盘,并且在指定的超时后将被擦除。默认值为900秒(15分钟)。
警告:如果使用此方法,您的Git帐户密码将以明文格式保存在global.gitconfig文件中,例如在Linux中,它将是/home/[用户名]/.gitconfig。
如果您不希望这样做,请为您的帐户使用ssh密钥。
您只需修改~/.git凭据
然后添加以下行:
git:https://<user>:<token/password>@gitlab.com
就是这样