如今很多公司使用git来作为代码版本控制工具,现所在公司是自己搭建私人的GitLab来管理代码的,GitLab中使用git clone下载源码时,支持https和git(即ssh)两种方式下载源码。

一、使用https方式下载源码

这种方法在每次clone、pull、push等操作时都需要输入账号密码,如此十分繁琐。后来找到解决方案,就是在本地git客户端生成ssh-key,配置到GitLab去。

git clone https://.../sora.git

二、使用Git方式(ssh-key)下载源码

如果使用这种方法时,没配置SSH key,会报如下错误。

$ git clone ssh://git@ip:port/username/proj.git
Cloning into 'proj'...
git@ip: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

配置SSH证书的方法:

1. 使用ssh-keygen命令,生成ssh公钥和私钥对

点击回车,会让你选择存储路径,此时不用理会直接回车,其会保存到默认路径。

¥ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key (/home/msy/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/msy/.ssh/id_rsa.
Your public key has been saved in /home/msy/.ssh/id_rsa.pub.
The key fingerprint is:SHA256:***************
The key's randomart image is:***************

2. 查看并复制公钥

cat /home/msy/.ssh/id_rsa.pub

type %userprofile%\.ssh\id_rsa.pub | clip
# 此时已复制

3. 配置GitLab

到GitLab界面,点击settings,后点击SSH-Keys,将复制过来的公钥粘贴到key框中,下方title可自己命名,点击addkey即可。


经过如上操作,再次拉取代码和提取代码应无需再输密码,若还需输入密码

      输入 git config --global credential.helper store  命令

      然后操作pull/push 会让输入用户名密码,第一次输入进去。下次再操作pull/push时就不需要输入用户名密码了。

参考文献:

git ssh key配置_讨厌走开啦-CSDN博客_git ssh

更多推荐

GitLab下载源码的两种方式(https/ssh-key)