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

那么我如何在Git中保存我的凭据?


当前回答

打开凭据助手,以便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帮助。

其他回答

对于全局设置,打开终端(从任何位置),运行以下命令:

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

这样,您计算机上的任何本地Git存储库都将使用该信息。

您可以通过执行以下操作为每个存储库单独配置:

在存储库文件夹中打开终端。运行以下操作:git-config user.name“您的用户名”git-config user.password“您的密码”

它只影响该文件夹(因为您的配置是本地的)。

除了编辑~/.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用户保存配置参数。

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

全局保存用户名和密码:

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

在浏览了数十篇StackOverflow文章、博客等之后,我尝试了每一种方法,这就是我想到的。它涵盖了一切。

普通的DevOps Git证书和私有包备忘单

这些都是在没有交互式密码提示的情况下安全验证Git以克隆存储库的所有方法和工具。

SSH公钥SSH-ASKPASSAPI访问令牌GIT_ASKPASS.gitconfig代替.gitconfig[凭据].git凭据.netrc文件私人套餐(免费)Node.js/npm包.jsonPython/pip/geggs requirements.txt红宝石宝石宝石文件转到.mod

银子弹

只想工作™? 这是神奇的银弹。

获取您的访问令牌(如果您需要GitHub或Gitea说明,请参阅备忘单中的部分),并将其设置在环境变量中(用于本地开发和部署):

MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

对于GitHub,逐字复制并运行以下行:

git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"

祝贺现在,无论是使用HTTPS还是SSH URL,任何自动克隆Git存储库的工具都不会受到密码提示的阻碍。

不使用GitHub?

对于其他平台(Gitea、GitHub和Bitbucket),只需更改URL即可。不要更改用户名(尽管是任意的,但不同的配置条目需要它们)。

兼容性

这在macOS、Linux、Windows(Bash)、Docker、CircleCI、Heroku、Akkeris等本地运行。

更多信息

请参阅备忘单的“.gitconfig insteadOf”部分。

安全

请参阅备忘单的“安全”部分。