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

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


当前回答

我认为缓存凭据比永久存储更安全:

git config --global credential.helper 'cache --timeout=10800'

现在,您可以输入用户名和密码(git pull或…),并在接下来的三个小时内继续使用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'

全局保存用户名和密码:

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

如果您正在Windows上使用Git凭据管理器。。。

git-config-l应该显示:

credential.helper=manager

但是,如果没有提示您输入凭据,请执行以下步骤:

从“开始”菜单打开“控制面板”选择用户帐户在左侧菜单中选择管理凭据删除与Git或GitHub相关的任何凭据

如果您有代理并且Git服务器位于内部网络上,请确保您没有设置HTTP_PROXY、HTTPS_PROXY、NO_PROXY环境变量。

您还可以使用gitgui测试Git fetch/push/pull,gitgui链接到C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\Git-core中的凭据管理器二进制文件

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

检查

git config --list

您的问题:

我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,而每次都不需要在提示中输入用户名和密码。那么我如何在Git中保存我的凭据?

如果您是github或其他提供商,我建议不要将它们保存在Git中,例如~/.Git凭据,而是将它们视为加密的机密。

将凭据设置为以下格式:

https://your_user_name:your_token@github.com/your_user_name/your_repo_name.git

将其作为加密机密,如secrets.REPOSTORY:

然后,您可以使用它来克隆公共或私有回购及其子模块,以及自动执行推拉操作

# put the credentials in a variable
export REPOSITORY=${{ secrets.REPOSITORY }}

# git clone public or private repos
git clone --recurse-submodules -j8 ${REPOSITORY}

# git pull will do automatic
cd /path/to/the/repo
git pull

# git push to a branch in the repo
git add . && \
  git commit -m "Action from ${GITHUB_SHA}" && \
  git push --force $REPOSITORY master:$BRANCH