方案一[推薦]
設(shè)置步驟:
本地進(jìn)入.ssh查看是否存在密鑰對(duì):xxx和xxx.pub
命令:cd ~/.ssh如果不存在灭美,使用ssh-keygen來(lái)創(chuàng)建
命令:ssh-keygen -t rsa -C "youremail@youremail.com"
例如:ssh-keygen -t rsa -C "youemail@163.com"
注解:
Enter file in which to save the key 輸入保存秘鑰的文件 直接enter即可
Enter passphrase (empty for no passphrase) 輸入密碼 直接enter即可
此時(shí)查看.ssh目錄下可看到新增的一對(duì)秘鑰id_rsa和id_rsa.pub查看公鑰
命令:cat ~/.ssh/id_rsa.pub
復(fù)制全部屈张,包括后面的郵箱添加到gitlab中
左側(cè)欄Profile Settings → 左側(cè)欄SSH Keys → 粘貼并Add key-
創(chuàng)建config剩胁,端口為22可忽略這一步
命令:cat>~/.ssh/config
輸入:Host gitlab.xxx.com User git Port 458 IdentityFile /home/yourname/.ssh/id_rsa(替換成你的id_rsa所在的路徑诉植,也可以不要)
-
更改remote
使用git remote -v
查看origin和upstream的url,把http更改為ssh地址昵观,命令:
git remote set-url origin(或者upstream) xxxx
例如:git remote set-url origin git@gitlab.xxxx.com:xxx/server.git git remote set-url upstream git@gitlab.xxxx.com:enterprise/server.git
驗(yàn)證是否設(shè)置成功
命令:ssh -T git@gitlab.xxxxxxxx.com
顯示W(wǎng)elcome to GitLab, yourname! 代表成功晾腔。
總結(jié)
以上是http改為ssh舌稀,若是一開始clone就是以ssh方式訪問(wèn),方法見下:
同樣第5步根據(jù)需要選擇配置與否灼擂。
第6步改為
git clone git@gitlab.xxxx.com:xxx/server.git
clone遠(yuǎn)程倉(cāng)庫(kù)到本地壁查,稱為origin
git remote add upstream http://gitlab.xxxx.com/enterprise/server.git
添加upstream
方案二
使用git clone
命令clone
項(xiàng)目時(shí),如果repository
的SSH
端口不是標(biāo)準(zhǔn)22
端口時(shí)(例如剔应,SSH tunnel
模式睡腿,等等),可以使用如下命令:
git clone ssh://git@hostname:port/.../xxx.git
舉例如下:
git clone ssh://git@10.137.20.113:2222/root/test.git
# 后面的/var/opt/gitlab/git-data/repositories/developer/approve.git 是代碼所有在的服務(wù)器路徑
git clone ssh://root@42.62.11.190:4222/var/opt/gitlab/git-data/repositories/developer/approve.git