Git 保存远程凭证(HTTPS 方式,保存账号/密码)

仅适用于 https:// 远程地址;SSH 是密钥体系,不是保存密码。

方案1:cache(内存临时缓存,推荐,无明文落盘)

默认缓存15分钟,重启终端失效,不会写到硬盘文件

# 全局配置
git config --global credential.helper cache
# 自定义缓存时长,单位秒,例:缓存1小时
git config --global credential.helper 'cache --timeout=3600'

设置后,第一次 pull/push 输入账号密码,有效期内不再重复输入。

方案2:store(永久保存,明文文件,适合PVE/Linux服务器)

⚠️ 会把账号密码明文保存在 ~/.git-credentials,本机其他人能读到,安全风险

git config --global credential.helper store

首次执行git操作输入账号密码,成功后自动写入文件,永久生效。

查看保存的凭证:

cat ~/.git-credentials

内容格式:https://用户名:密码@git地址

删除保存的密码(改密码后用)

rm ~/.git-credentials

方案3:系统凭证管理器(桌面系统,Windows/macOS)

# Windows
git config --global credential.helper wincred
# Mac
git config --global credential.helper osxkeychain

密码存在系统安全仓库,不是明文文件,安全性更高。

查看当前凭证配置

git config --global --get credential.helper

快速切换远程地址并附带账号密码(临时,不推荐长期)

git remote set-url origin [https://yourname:yourpassword@git.xxx.com/repo.git](https://yourname:yourpassword@git.xxx.com/repo.git)

缺点:密码会写进git配置,.git/config能看到明文,仅测试临时用。

补充:如果你是SSH远程服务器(不是git https)

如果你是ssh登录Linux服务器想要免密,不是git密码,用ssh密钥:

ssh-keygen -t ed25519
ssh-copy-id root@服务器IP

之后ssh登录不用输密码。

作者:月影鹏鹏  创建时间:2026-10-04 08:22
最后编辑:月影鹏鹏  更新时间:2026-10-04 08:22