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 

posted @ 2019-03-20 22:59  舞羊  阅读(24777)  评论(0编辑  收藏  举报