git兩種連接方式:ssh和http配置介紹


在管理Git項目上,很多時候都是直接使用https url克隆到本地,當然也有有些人使用SSH url克隆到本地。這兩種方式的主要區別在於:使用https url克隆對初學者來說會比較方便,復制https url然后到git Bash里面直接用clone命令克隆到本地就好了,但是每次fetch和push代碼都需要輸入賬號和密碼,這也是https方式的麻煩之處。而使用SSH url克隆卻需要在克隆之前先配置和添加好SSH key,因此,如果你想要使用SSH url克隆的話,你必須是這個項目的擁有者。否則你是無法添加SSH key的,另外ssh默認是每次fetch和push代碼都不需要輸入賬號和密碼,如果你想要每次都輸入賬號密碼才能進行fetch和push也可以另外進行設置。

gitlab環境部署好后,創建project工程,在本地或遠程下載gitlab代碼,有兩種方式:ssh和http

1)ssh方式:這是一種相對安全的方式
這要求將本地的公鑰上傳到gitlab中,如下圖:

 

window客戶機設置ssh方式連接gitlab,請見:http://www.cnblogs.com/kevingrace/p/5651402.html(文章底部有介紹)

2)http連接方式

這種方式要求project在創建的時候只能選擇“Public”公開狀態,Private和Internal私有模式下不能使用http方式進行連接。(ssh方式在三種模式下都可以)。使用http方式直接連接gitlab顯然沒有ssh連接方式安全,但是也可以做些安全設置,比如在gitlab本機的iptables里做端口限制(如上是8081端口),添加白名單等。

 

另外需要注意的是:gitlab上創建的項目倉庫,要注意該倉庫下的members權限,如果某個gitlab用戶沒有設置在該倉庫members權限下,則使用該gitlab用戶進行git clone操作可以,但是進行git push則會失敗!報錯:remote: GitLab: You are not allowed to push code to protected branches on this project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
比如,使用gitlab的kevin用戶進行git上傳下載操作,kevin用戶默認沒有加到scanhost.git項目倉庫的member權限下。
1)git clone是可以的
[root@ test  ~] # git config --global user.name "王士博"
[root@ test  ~] # git config --global user.email "kevin@veredholdings.com"
[root@ test  ~] # git clone http://gitlab.kevin-inc.com/fanglianchao/scanhost.git
[root@ test  ~] # cd scanhost
 
2)git push則不允許,沒有權限
[root@ test  ~] # touch README.md
[root@ test  ~] # git add README.md
[root@ test  ~] # git commit -m "add README"
[root@ test  ~] # git push -u origin master
.......
remote: GitLab: You are not allowed to push code to protected branches on this project.
To http: //gitlab .kevin-inc.com /fanglianchao/scanhost .git
  ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to  'http://gitlab.kevin-inc.com/fanglianchao/scanhost.git'
 
需要將kevin用戶加入到scanhost.git項目倉庫的member權限里,這樣就有權限進行git push了!

參考文章:https://www.cnblogs.com/kevingrace/p/6114810.html 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM