我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
当前回答
除了编辑~/.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用户保存配置参数。
其他回答
对于有相同问题的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密码更好(例如,在远程共享计算机中可能不安全)
如果您使用SSH身份验证而不是用户名/密码身份验证,您将更加安全。
如果您使用的是Mac,SSH客户端身份验证将集成到macOS密钥链中。创建SSH密钥后,在终端中键入:
ssh-add -K ~/.ssh/id_rsa
这将向macOS密钥链添加SSH私钥。Git客户端连接到远程服务器时将使用SSH。只要您在服务器上注册了ssh公钥,就可以了。
全局保存用户名和密码:
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,然后按照提示进行操作。
推荐的安全方法:SSH
创建SSH GitHub密钥。转到github.com→ 设置→ SSH和GPG密钥→ 新SSH密钥。现在将您的私钥保存到计算机。
然后,如果私钥保存为~/.ssh/目录中的id_rsa,我们将其添加为身份验证:
ssh-add -K ~/.ssh/id_rsa
更安全的方法:缓存
我们可以使用git凭据缓存在一段时间内缓存用户名和密码。只需在CLI(终端或命令提示符)中输入以下内容:
git config --global credential.helper cache
您还可以按如下方式设置超时时间(以秒为单位):
git config --global credential.helper 'cache --timeout=3600'