0


gitlab 分支保护

一、创建用户

在这里插入图片描述

1.填写用户信息

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pyMSD8uR-1650354069518)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183234249.png)]

2.分配权限

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CO4qN9IL-1650354069519)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183332332.png)]

3.修改用户初始密码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DqSML8cZ-1650354069520)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183600916.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ODzOesnf-1650354069520)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183628845.png)]

然后保存修改

二、将用户添加到指定的项目组

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T0TLOhvk-1650354069521)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183938365.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jR5zE0u9-1650354069522)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418184025415.png)]

这里就看到我们添加的张三用户了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MyjfSlBs-1650354069522)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418184139374.png)]

1.使用张三的身份克隆代码

[root@node-2 data]# git clone http://[email protected]/root/taikang_test.git
Cloning into 'taikang_test'...
Password for'http://[email protected]': 
remote: Counting objects: 3, done.
remote: Total 3(delta 0), reused 0(delta 0)
Unpacking objects: 100% (3/3), done.

[root@node-2 data]# cd taikang_test/[root@node-2 taikang_test]# ls
readme  rootv1
[root@node-2 taikang_test]# touch zhangsanv1[root@node-2 taikang_test]# ls
readme  rootv1  zhangsanv1
[root@node-2 taikang_test]# git add -A[root@node-2 taikang_test]# git commit -m 'zhangsanv1'[master 352056b] zhangsanv1
 1file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 zhangsanv1

2.提交代码

[root@node-2 taikang_test]# git push origin master
Password for'http://[email protected]': 
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 274 bytes |0 bytes/s, done.
Total 2(delta 0), reused 0(delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To http://[email protected]/root/taikang_test.git
 ![remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://[email protected]/root/taikang_test.git'

这时候我们提交代码报了一个错误

2.1 全局分支保护

这是因为默认 master 分支是处于被保护状态下的,develop 角色是没有权限提交到 master 分支的。这是全局配置的分支保护
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7rErezfR-1650354069523)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418191511945.png)]
完全保护:开发人员不能推送提交,只有主程序员可以。不允许任何人强制推送或删除分支

2.2 项目分支保护功能

在每一个项目中都可以设置属于自己的项目的分支保护
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HsR85cNJ-1650354069525)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419131043044.png)]
由此可以看出上边提交代码错误,是因为zhangsan是开发者,没有权限提交到gitlab代码库

3.修改用户提交权限

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wFi39BYv-1650354069526)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419131836216.png)]
这样就开发者也就可以提交到master分支了。或者把zhangsan直接变成

[root@node-2 taikang_test]# git add -A[root@node-2 taikang_test]# git commit -m 'zhangsanv1'[master 3a22927] zhangsanv1
 1file changed, 1 insertion(+)[root@node-2 taikang_test]# git push origin  master
Password for'http://[email protected]': 
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 524 bytes |0 bytes/s, done.
Total 6(delta 0), reused 0(delta 0)
To http://[email protected]/root/taikang_test.git
   2960619..3a22927  master -> master

三、生产中分支保护设置

在实际生产中 会设置一个test的分支,允许开发和主程序员同时push代码。但是master只能主程序员可以push.然后在将test分支合并到master分支上
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7qszIedD-1650354069528)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419132603411.png)]

四、复现生产分支保护设置

1.新建分支

图1: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yGnQZKEr-1650354069529)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133508536.png)]
图2:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GQACLAc9-1650354069530)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133422117.png)]

图3:分支已经创建完成
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JJo3FoIh-1650354069531)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133604485.png)]

2.分支上新建文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NA2IUTyj-1650354069532)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133834215.png)]

3.客户端克隆仓库

#以zhangsan用户的身份克隆仓库[root@node-2 data]# git clone http://[email protected]/root/taikang_test.git
Cloning into 'taikang_test'...
Password for'http://[email protected]': 
remote: Counting objects: 26, done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 26(delta 3), reused 11(delta 2)
Unpacking objects: 100% (26/26), done.

虽然刚克隆下来的时候没有test-500分支,其实已经克隆下来了 直接切换分支即可。这里发现test-500文件也存在

[root@node-2 data]# cd taikang_test/[root@node-2 taikang_test]# ls
readme  zhangsanv1
[root@node-2 taikang_test]# git branch
* master
[root@node-2 taikang_test]# git checkout test-500
Branch test-500 set up to track remote branch test-500 from origin.
Switched to a new branch 'test-500'[root@node-2 taikang_test]# ls
readme  test-500  zhangsanv1

在test-500分支上创建新文件在提交

[root@node-2 taikang_test]# touch test01[root@node-2 taikang_test]# echo "test01" > test01[root@node-2 taikang_test]# git add -A[root@node-2 taikang_test]# git commit -m 'test01'[test-500 5e6f503] test01
 1file changed, 1 insertion(+)
 create mode 100644 test01

[root@node-2 taikang_test]# git push origin test-500
Password for'http://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 332 bytes |0 bytes/s, done.
Total 3(delta 0), reused 0(delta 0)
remote: 
remote: To create a merge request for test-500, visit:
remote:   http://pangbing.com/root/taikang_test/merge_requests/new?merge_request%5Bsource_branch%5D=test-500
remote: 
To http://[email protected]/root/taikang_test.git
   f358476..5e6f503  test-500 -> test-500

4.以zhangsan的身份登录gitlab

图1:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RZfJLPx5-1650354069533)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419144406876.png)]
图2:

在这里发现 系统会自动提示在taikang_test项目中有可以合并分支的请求
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mff6HMe9-1650354069534)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419144518190.png)]
当然点击项目进入项目主页之后也会看到 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wIdKaNbq-1650354069535)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152027022.png)]

5.发送合并代码请求

图1: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QCXynoxt-1650354069537)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152122985.png)]
图2:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FgqUKGoR-1650354069538)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152206112.png)]

6.使用管理员登录gitlab 处理合并请求

在这里插入图片描述
图2:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FkOSsz0f-1650354069540)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152955265.png)]
图3:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EmbeMH9D-1650354069541)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419153016384.png)]
图4:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JempBlFm-1650354069542)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419153057031.png)]

7.验证合并

此时我们发现在master上已经有test-500分支上的文件了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FI7xiRd4-1650354069542)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419153218252.png)]

标签: git

本文转载自: https://blog.csdn.net/smile_pbb/article/details/124275499
版权归原作者 smile_pbb 所有, 如有侵权,请联系我们删除。

“gitlab 分支保护”的评论:

还没有评论