配置GitLab访问秘钥

2,570 阅读3分钟

安装Git

下回分解

开通GitLab账号

咨询公司GitLab管理员给你开通账号,本地Git仓库和GitLab仓库不能传输项目,需要通过SSH加密才能传输,让GitLab认证本地的SSH Key

Git生成SSH Key

  1. 检查本机是否有SSH Key设置
cd .ssh

如果没有则提示:No such file or directory 如果有则进入ssh路径下(dir查看当前路径文件,rm*删除所有文件)

dir

以上是有配置过SSH秘钥的,已配置的秘钥不能链接远程的话建议删除所有文件重新配置

  1. 生成SSH Key:
  • 使用Git Bash生成新的ssh key
  • cd ~ #保证当前路径在”~”下
  • 输入命令 ssh-keygen -t rsa -C "your_email@youremail.com"
注意在回车后会提示一些东西可以不填,命令中的email,就是gitlab中的账号,需要保持一致
Generating public/private rsa key pair.Enter file in which to save the key (/c/Users/xxxx_000/.ssh/id_rsa):   #不填直接回车
Enter passphrase (empty for no passphrase):   #输入密码(可以为空)
Enter same passphrase again:   #再次确认密码(可以为空)Your identification has been saved in /c/Users/xxxx_000/.ssh/id_rsa.   #生成的密钥Your public key has been saved in /c/Users/xxxx_000/.ssh/id_rsa.pub.  #生成的公钥
The key fingerprint is:e3:51:33:xx:xx:xx:xx:xxx:61:28:83:e2:81 xxxxxx@yy.com

3. 添加ssh key到GItHub

  • 登录GitHub系统;点击右上角账号头像的“▼”→Settings→SSH kyes→Add SSH key。

  • 把以.pub的文件打开复制出来 放在上图的key框中

4. 全局设置

git config --global user.name “your_username”  #设置用户名
$ git config --global user.email “your_registered_github_Email”  #设置邮箱地址
  1. 测试ssh keys是否设置成功。
ssh -T git@gitlab.dinglc.com.cn这个是你项目的存放地址
The authenticity of host 'gitlab.dinglc.com.cn (139.224.114.55)' can't be established.
ECDSA key fingerprint is SHA256:9rE8HXNT+n1orODkm/85Zer9+vI1oeqiOV3KMdopgjI.Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.dinglc.com.cn,139.224.114.55' (ECDSA) to the list of known hosts.
Welcome to GitLab, 李鹏!
Enter passphrase for key '/c/Users/xxxx_000/.ssh/id_rsa':  #生成ssh kye是密码为空则无此项,若设置有密码则有此项且,输入生成ssh key时设置的密码即可。
  1. Hi xxx! You've successfully authenticated, but GitHub does not provide shell access. #出现这句话,说明设置成功

Git示例

注意:克隆代码之前确保有相关的项目代码权限(master、developer、reporter),如无权限,请求项目具有master权限的同事帮忙分配权限

  • 这里以verify-center项目为例(有相关权限的项目,登录gitlab后,首页右侧会全部以列表形式展示),点击verify-center获取SSH 链接地址

  • 在本地目标下载目录下,右键-->Git Bash Here
  • 输入命令:git clone git@gitlab.baofoo.net:clearing/verify-center.git
  • 首次拉取代码时,需要确认秘钥信息,输入yes即可
  • 确认项目已从gitlab上克隆到本地
项目拉取完成,可以打开使用了....