我想在GitExtensions、Sourcetree或任何其他GitGUI中自动使用推拉,每次都不需要在提示中输入用户名和密码。
那么我如何在Git中保存我的凭据?
我想在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或其他
请参阅文档。
重新启动终端。
其他回答
您的问题:
我想在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将您的密码保存在内存中一段时间:
在终端中,输入以下内容:
# 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帮助。
对于有相同问题的gitlab用户-
您可以设置一个部署令牌来克隆或从存储库中提取(不能使用部署令牌将代码推送到repo)。
在这里,您可以了解有关gitlab部署令牌的更多信息:https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html
创建部署令牌后,请使用以下方法克隆回购:
git clone https://${username}:${deploy_token}@gitlab.com/yourusername/yourreponame.git
我认为这种方法比全局保存git用户名和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将凭据存储在磁盘上。
您可以编辑~/.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或其他
请参阅文档。
重新启动终端。