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

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


当前回答

您可以编辑~/.gitconfig文件以存储凭据:

sudo nano ~/.gitconfig

应该已经有了

[user]
        email = your@email.com
        user = gitUSER

您应该在该文件的底部添加以下内容。

[credential]
        helper = store

我推荐这个选项的原因是因为它是全局的,如果在任何时候你需要删除这个选项,你就知道该去哪里更改它。

仅在个人计算机中使用此选项。

然后,当您拉|clone|输入Git密码时,通常,密码将以以下格式保存在~/.Git凭据中

https://gituser:gitpassword@domain.xxx

其中DOMAIN.XXX可以是github.com、bitbucket.org或其他

请参阅文档。

重新启动终端。

其他回答

只需将您的凭据放入URL,如下所示:

https://Username`**:**`Password`**@**`github.com/myRepoDir/myRepo.git`

您可以这样存储:

git remote add myrepo https://Userna...

…使用它的示例:

git push myrepo master`

现在就是列出URL别名:

git remote -v

…以及删除其中一个的命令:

git remote rm myrepo

截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!

安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码浏览器内OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。

推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。

外观配置(文档):

首选在终端而不是GUI中选择身份验证方法(点击次数较少)始终使用浏览器方法,而不是每次都被询问(甚至更少的按键)

git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser

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

您的问题:

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