在 Linux 上使用 GitLab 并通过 SSH 提交/拉取代码,一般步骤如下:
在终端执行:
ssh-keygen -t ed25519 -C "your_email@example.com"
如果系统较老不支持
ed25519,可以用:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
一路回车即可(默认会生成在):
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
cat ~/.ssh/id_ed25519.pub
复制输出的内容(以 ssh-ed25519 开头)。
ssh -T git@gitlab.com
如果是自建 GitLab:
ssh -T git@your.gitlab.domain
看到类似:
Welcome to GitLab, @username!
说明成功。
在 GitLab 项目页面:
git clone git@gitlab.com:username/project.git
git remote set-url origin git@gitlab.com:username/project.git
查看:
git remote -v
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
可配置 ~/.ssh/config:
Host gitlab-work
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_work
然后使用:
git clone git@gitlab-work:username/project.git
如果你用的是 自建 GitLab / 公司内网 / Docker GitLab,可以告诉我具体环境,我可以给你更精确的配置。