我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。

那么我如何在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文档:

如果使用SSH传输连接到远程设备您可以使用不带密码短语的密钥无需输入用户名和密码即可安全地传输数据。然而,这在HTTP协议中是不可能的——每个连接需要用户名和密码。这对于具有双因素身份验证的系统,其中用于密码是随机生成的,无法读出。幸运的是,Git有一个证书系统可以帮助解决这个问题。Git公司框中提供了几个选项:默认情况下根本不缓存。每个连接都会提示您输入您的用户名和密码。“缓存”模式将凭据保存在内存中一段时间时间没有一个密码存储在磁盘上15分钟后从缓存中清除。“存储”模式将凭证保存到磁盘上的纯文本文件中,它们永远不会过期。这意味着在您更改密码之前对于Git主机,您不必输入凭据再一次这种方法的缺点是存储了密码在主目录中的明文文件中。如果您使用的是Mac,Git带有“osxkeychain”模式在连接到您的系统帐户。此方法将凭据存储在磁盘上永不过期,但它们使用存储HTTPS证书和Safari自动填充。如果您使用的是Windows,可以安装一个名为“Git”的助手Windows凭据管理器。“这类似于“osxkeychain”上面描述的助手,但使用Windows凭据存储控制敏感信息。可以在https://github.com/Microsoft/Git-Credential-Manager-for-Windows.您可以通过设置Git配置来选择以下方法之一值:

git config --global credential.helper cache

git config --global credential.helper store

从7.14 Git工具-凭据存储

推荐的安全方法: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'

要将用户名和用户密码保存到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帐户中

GitHub的建议现在已经改变,最好的方法也是最简单的。此处显示详细信息

安装Github官方cli。例如,对于mac:brew install gh。在终端中键入gh-auth-login,然后按照提示进行操作。

git config --global user.name "your username"
git config --global user.password "your password"

检查

git config --list