240
世界上有10种人,一种懂二进制,另一种不懂二进制。

Gitlab的SSH配置(linux和windows双版本)

1. 步骤
1.首先现在电脑端安装好git,windows端请安装Git for Windows,Linux端请自行网上查询(Ubuntu: sudo apt-get install git)
 
2.先核对下电脑上是已经有ssh配置
#Git Bash on Windows / GNU/Linux / macOS / PowerShell:
cat ~/.ssh/id_rsa.pub
 
3.若没有,则需要生成ssh的公钥私钥
Git Bash on Windows / GNU/Linux / macOS:
##若要自定义id_rsa文件请先切换目录到 ~/.ssh/下,如果不切换,当保存的文件名是自定义时,会生成在当前的目录下。
##经测试,邮箱不一定是登录gitlab的邮箱(我github也是用mygitlab@gitlab.com生成的id_rsa_pub作为公钥)
ssh-keygen -t rsa -C your.email@example.com -b 4096
#提示是否使用新的文件名,如果不输入新的文件名,则生成id_rsa文件。
##如果默认不配置config,就得默认为id_rsa文件名
Enter file in which to save the key (~/.ssh/id_rsa):
#请输入确认密码,后面还会用到(至少4位数),如果缺省直接按回车
##此密码是验证id_rsa的密码,每次代码commit时得输入
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
 
4. 复制公钥到GitLab中的SSH Keys中
Windows Command Line:
type %userprofile%\.ssh\id_rsa.pub | clip 
Git Bash on Windows / Windows PowerShell:
cat ~/.ssh/id_rsa.pub | clip 
如果提示clip没安装,则只要‘ |’ 的前半部分命令,然后复制出文本,或者直接到指定位置用文本打开。
 
5.测试SSH是否已配置好
ssh -T git@gitlab.com
#如果已经配置好,则会提示
Welcome to GitLab, Your GitLab NickName!
 
6.配置config文件(可选,当第5步骤失败)
如果id_rsa是自己定义为id_rsa_custom的,才需要配置config,具体见参考[1]
在~/.ssh/下面新建一个文件
cd ~/.ssh/
touch config

config内容如下: 

# GitLab.com server
Host gitlab
Hostname gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_custom
 
ssh -T git@gitlab
#如果已经配置好,则会提示
Welcome to GitLab, Your GitLab NickName!

  

7.Clone项目到本地
如果gitlab上已经有Repo(项目),则直接clone一份代码到本地
#如果用SSH clone失败了,请尝试用HTTPS clone
##目前只在company的服务器ubuntu系统失效了
git clone git@gitlab.com:username/yourProject.git #(SSH方式)
git clone https://gitlab.com/username/yourProject.git #(HTTPS方式)
#此时会出现登录gitlab的账号和密码的输入,然后显示进度条
Receiving objects:  86% (797/918), 2.48 MiB | 5.00 KiB/s

如果gitlab还没有Repo,可以新建一个,再clone空项目下来。

按理说github设置SSH原理是完全相同的。

p.s.

如果git还没有配置过用户名和邮箱需要设置一下

##用户名和邮箱名和账号名没有必然相关性,可以不一样
##我用github的账号设置了user.email,然而gitlab照样可以push数据
##首次clone数据时有要求输入该网站的账号和密码,可以理解git的配置是git的账号和昵称
git config --global user.name "Your Name" 
git config --global user.email your_email@gmail.com 

 

2. 可能出现的情况

在windows系统下用git bash,在第5步骤,一直提示超时连接,不管是ssh还是https协议。(以上两项暂时只出现在company的PC)
posted @ 2017-11-02 16:16  unionline  阅读(10714)  评论(0编辑  收藏  举报