Macbook使用Gitlab配置SSH Key

git是分布式代码管理工具,远程代码管理是基于ssh的,代码上传大搜gitlab或者github代码仓储时,需要进行ssh配置。

把本地代码上传到服务器时需要加密处理,git中公钥(id_rsa.pub)是用来加密的数字,用私钥(id_rsa)来还原,gitlab/github发回来的使用公钥加密过的数据,用本地的私钥来还原。

如果公钥或者私钥丢了一个,就不能用了,重新生成一次把之前的覆盖掉,然后再gitlab/github中再设置一次即可。

电脑终端运行:

1. 查询ssh key是否存在

ls -al !/.ssh

2.复制id_rsa.pub,将复制的key粘贴到gitlab/github ssh设置中

pbcopy < ~/.ssh/id_rsa.pub

3.生成ssh key

#  -t 为指定加密方式为RSA, -C 为指定邮箱。命令完成后目录下会生成id_rsa(私钥)和id_rsa.pub(公钥)
ssh-keygen -t rsa -C "your_email@example.com"

 默认会在相应路径下(/your_home_path)生成id_rsa和id_rsa.pub两个文件,此时终端会显示:

Generating public/private rsa key pair. 

Enter file in which to save the key (/your_home_path/.ssh/id_rsa): 

可能会让输入开机密码,可能不会直接回车即可。

4.打印公钥:

cat id_rsa.pub

ssh key 配置到github/gitlab之后,根据此仓储的配置提示,进行本机的配置:全局配置、上传本地代码库、新建项目等等。

如:

git config --global user.name "xxxx"
git config --global user.email "xxxx@163.com"
git init
git remote add origin xxxx(git通道)
git add .
git commit -m "11.11 11:11 update"
git push -u origin master
原文地址:https://www.cnblogs.com/belle-ls/p/gitlab-ssh.html