git config --global credential.helper store是最直接的免密方式,它将https凭证明文存入~/.git-credentials,首次git pull需手动输入账号密码,之后自动复用;适用于个人开发机等低安全要求场景,但禁用于共享机器。

git config credential.helper store 是最直接的免密方式
它让 Git 把账号密码明文存到本地文件,后续 git pull 或 git push 就不再弹提示。适合个人开发机、测试环境等对安全性要求不高的场景。
执行这行命令即可生效:
git config --global credential.helper store
注意两点:
- 加了
--global是全局生效,所有仓库都走这个配置;不加则只对当前目录下的仓库有效 - 执行后不会立刻免密,要等下一次
git pull或git push时手动输一次账号密码,Git 才会把凭证写进~/.git-credentials - 写入的密码是明文,
cat ~/.git-credentials能直接看到https://username:password@github.com这种格式,别用在共享服务器或多人共用机器上
HTTPS 仓库 URL 是反复输密码的根本原因
如果你用的是 https://github.com/username/repo.git 这类地址克隆的仓库,Git 默认每次操作都要向服务器发起 HTTPS 认证请求,而服务器不记你上次输过啥——所以它必须问。
解决思路只有两个方向:
- 让 Git 自己“记住”这次输的凭证(即上面的
credential.helper store) - 换协议:把远程地址改成 SSH,靠密钥对认证,彻底绕开密码输入环节
查当前远程地址用:git remote -v;如果是 https:// 开头,就属于这个范畴。
改用 SSH URL 后,credential.helper 就没用了
SSH 协议不走 HTTP Basic Auth,所以 credential.helper 配置对 git@github.com:username/repo.git 这类地址完全无效。它只管 HTTPS。
切换步骤很简单:
- 先生成密钥对:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"(一路回车就行) - 把公钥内容(
cat ~/.ssh/id_rsa.pub输出)粘贴到 GitHub/GitLab/Gitee 的 SSH Keys 设置页 - 改远程地址:
git remote set-url origin git@github.com:username/repo.git
之后第一次 git push 可能提示 “The authenticity of host 'github.com' can't be established”,输入 yes 回车就行,这是正常校验,不是密码提示。
.git-credentials 文件权限容易被忽略
用 store 方式时,Git 会在用户主目录下生成 .git-credentials,里面存着明文账号密码。Linux/macOS 下如果这个文件权限太宽(比如 644 或 755),Git 会直接拒绝读取,导致免密失效,又跳回输密码界面。
修复方法只有一句:
chmod 600 ~/.git-credentials
Windows 上虽然不校验权限,但如果你用 WSL 或跨平台工具同步了该文件,也建议统一设为 600,避免在子系统里出问题。











