在 Linux 上使用 GitLab 实现免密操作(拉取/推送代码不需要输入账号密码),常见有 3 种方式,最推荐的是 SSH 方式。
ssh-keygen -t ed25519 -C "your_email@example.com"
(如果系统旧,可用 rsa:ssh-keygen -t rsa -b 4096)
一路回车即可,默认生成在:
~/.ssh/id_ed25519.pub
cat ~/.ssh/id_ed25519.pub
复制输出内容,在 GitLab 中: User → Preferences → SSH Keys → Add Key
ssh -T git@gitlab.com
(如果是自建 GitLab,替换为你的域名,如 git@gitlab.yourcompany.com)
看到欢迎信息即成功。
git clone git@gitlab.com:username/project.git
✅ 之后 push / pull 都不再需要密码
如果你必须用 HTTPS 地址:
git config --global credential.helper cache
默认缓存 15 分钟
git config --global credential.helper store
账号密码会明文保存在:
~/.git-credentials
GitLab 已逐步禁止密码登录 HTTPS
GitLab: User → Preferences → Access Tokens
git clone https://<token>@gitlab.com/username/project.git
或第一次输入:
| 方式 | 推荐度 | 说明 |
|---|---|---|
| SSH | ⭐⭐⭐⭐⭐ | 最安全、最方便 |
| Token + HTTPS | ⭐⭐⭐ | 必须 HTTPS 时用 |
| 密码 | ❌ | GitLab 已不支持 |
如果你告诉我:
我可以给你更精确的命令。