CentOS Git HTTPS 凭证保存配置

每次执行 `git pull` 都需要输入账号和密码,记录账号密码不再输入。

6 热度
2 浏览

首先看下自己的仓库是否为HTTPS

执行以下命令:

bash 复制代码
git remote -v

永久保存账号凭证

执行以下命令:

bash 复制代码
git config --global credential.helper store

然后再执行一次:

bash 复制代码
git pull

根据提示输入 Git 平台的用户名和密码。

如果使用 GitHub,密码位置通常需要填写 Personal Access Token,而不是 GitHub 登录密码。

验证成功后,后续执行以下命令时一般不再要求输入账号密码:

bash 复制代码
git pull
git push

查看已保存的凭证

凭证通常保存在当前 Linux 用户的主目录:

bash 复制代码
cat ~/.git-credentials

如果当前使用的是 root 用户,文件路径通常为:

text 复制代码
/root/.git-credentials

查看当前配置

bash 复制代码
git config --global --get credential.helper

正常情况下会输出:

text 复制代码
store

安全提醒

credential.helper store 会以明文形式保存账号或访问令牌。

建议:

  • 服务器仅由自己使用时,可以采用该方式。
  • 多人共用服务器时,建议改用 SSH 密钥。
  • 不要将 .git-credentials 文件提交到 Git 仓库。

可以限制凭证文件权限:

bash 复制代码
chmod 600 ~/.git-credentials

临时记住凭证

如果不想永久保存,可以设置缓存时间,例如记住 24 小时:

bash 复制代码
git config --global credential.helper 'cache --timeout=86400'

其中 86400 的单位是秒。

取消保存

取消全局凭证保存配置:

bash 复制代码
git config --global --unset credential.helper

删除已经保存的凭证:

bash 复制代码
rm -f ~/.git-credentials
CentOS Git HTTPS 凭证保存配置

声明:Web前端小站 - 前端博客 - 王搏的个人博客|版权所有,违者必究|如未注明,均为原创

转载:转载请注明原文链接 - CentOS Git HTTPS 凭证保存配置

评论 (0)

0/50
暂无评论,快来抢沙发吧~