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

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


当前回答

您可以使用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将凭据存储在磁盘上。

其他回答

推荐的安全方法: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的建议现在已经改变,最好的方法也是最简单的。此处显示详细信息

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

根据rofrol在Linux Ubuntu上的评论,根据这个答案,下面是如何在Ubuntu上实现的:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

其他一些发行版提供二进制文件,因此您不必构建它。

在OSX中,它通常“内置”了一个默认模块“osxkeychain”,因此您可以免费获得它。默认情况下,OS X内置和自制版本都有它。

您的问题:

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

在完整阅读了答案并尝试了这个问题的大部分答案之后,我终于找到了适合我的过程。我想分享它,以防有人不得不处理一个复杂的用例,但仍然不想像我一样浏览所有答案和gitcredentials、gitcredential store等手册页。

如果你(像我一样)必须使用几个不同的用户名/密码组合来处理来自多个提供商(GitLab、GitHub、Bitbucket等)的多个存储库,请在下面找到我建议的步骤。如果您只有一个帐户可以使用,那么最好使用git-config--globalcredential.helperstore或git-config--globaluser.name“您的用户名”等解决方案,这些解决方案在前面的回答中已经得到了很好的解释。

我的解决方案:

取消设置全局凭据帮助器,以防以前的一些实验阻碍:)git-config--全局--取消设置凭据s.helper移动到repo的根目录并禁用本地凭据助手(如果需要)cd/path/to/my/repogit-config--取消设置凭据帮助程序`创建一个文件以将回购凭证存储到git-config credential.helper“存储--文件~/.git_repo_credentials”注意:此命令在您的主目录中创建一个名为“.git_repo_credentials”的新文件,git将您的凭据存储到该文件中。如果不指定文件名,Git将使用默认的“.Git_credentials”。在这种情况下,只需发出以下命令即可:git-config credential.helper存储设置您的用户名git配置凭据。*.username my_user_name注意:如果您的存储库来自同一个提供商(例如GitLab),通常可以使用“*”。如果您的存储库由不同的提供程序托管,那么我建议为每个存储库显式设置到提供程序的链接,如下面的示例(对于GitLab):git配置凭据。https://gitlab.com.username我的用户名(_U)

此时,如果发出需要凭据的命令(例如git pull),将要求您输入与“my_user_name”对应的密码。这只需要一次,因为git将凭据存储到“.git_repo_credentials”中,并在后续访问时自动使用相同的数据。