我想在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帐户中

其他回答

对于Windows用户,请查看.gitconfig文件并检查为凭据助手配置了什么。如果您有以下内容。。。

[凭证“helperselector”]selected=绞车

您将在Windows凭据管理器中找到凭据。

您可以在那里编辑凭证。

注意:Wincred已被弃用,请参阅。。。

https://github.com/Microsoft/Git-Credential-Manager-for-Windows#notice-该项目不再处于警告状态

因此,您可能需要重新配置Git以使用内置的Git凭据管理器。。。

git config --global credential.helper manager

推荐的安全方法: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将您的密码保存在内存中一段时间:

在终端中,输入以下内容:

# 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帮助。

您的问题:

我想在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

您可以使用git凭据存储将未加密的密码存储在磁盘上,仅受文件系统权限的保护。

实例

git config credential.helper store
git push http://example.com/repo.git

Username: <type your username>
Password: <type your password>

[Several days later]

git push http://example.com/repo.git

[Your credentials are used automatically]

您可以检查存储在文件~/.git凭据中的凭据。

有关更多信息,请访问git credential store-Helper将凭据存储在磁盘上。