GitLab上配置SSH来拉取代码

GitLab上配置SSH来拉取代码

1. 生成 SSH 密钥(如果尚未生成)

打开终端(Linux/macOS)或 Git Bash(Windows),运行以下命令生成密钥:

ssh-keygen -t ed25519 -C "your_email@example.com"
  • 按回车确认默认保存路径(~/.ssh/id_ed25519)。
  • 如果不想设置密码保护密钥,直接按两次回车留空密码。
  • 生成成功后,公钥文件为 ~/.ssh/id_ed25519.pub

2. 将公钥添加到 GitLab

1.将公钥添加到 GitLab
cat ~/.ssh/id_ed25519.pub

选中输出的全部内容(以 ssh-ed25519 AAA... 开头,邮箱结尾)。

2. 登录 GitLab 账户
  • 进入 GitLab 网站,点击右上角头像 -> Preferences
  • 在左侧菜单选择 SSH Keys
3. 添加 SSH 公钥
  • 将复制的公钥粘贴到 Key 输入框中。
  • 填写可选的 Title(例如:My Laptop)。
  • 点击 Add key

3. 使用 SSH 拉取代码

1. 获取仓库的 SSH 地址
  • 在 GitLab 仓库页面,点击 Clone 按钮,选择 Clone with SSH
  • 地址格式为:git@gitlab.com:username/xxx.git
2. 克隆仓库
git clone git@gitlab.com:username/xxxx.git
  • 如果是首次连接 GitLab,会提示验证主机指纹,输入 yes 确认。
posted @ 2025-02-24 13:03  HuangBingQuan  阅读(2142)  评论(0)    收藏  举报