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

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


当前回答

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

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

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

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

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

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

其他回答

您的问题:

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

在浏览了数十篇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”部分。

安全

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

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

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

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

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

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

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

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

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