gitlab分支保护
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
目录
1.开发者无法推送代码到master分支案例
1.1 查看jasonyin2020角色
如上图所示,jasonyin2020用户的角色是开发者(Developer)。
与此同时,jasonyin2020用户属于dev组哟。
1.2 查看jasonyin2020有权限的相关项目
如上图所示,jasonyin2020用户对dev组的meta-data项目有权限哟。
1.3 使用jasonyin2020用户拉取meta-data项目所有分支到本地
(1)使用jasonyin2020用户拉取meta-data项目的master分支
[root@ubuntu11.yinzhengjie.com ~]# cd /tmp/
[root@ubuntu11.yinzhengjie.com tmp]#
[root@ubuntu11.yinzhengjie.com tmp]# git clone http://gitlab11.yinzhengjie.com/dev/meta-data.git
Cloning into 'meta-data'...
Username for 'http://gitlab11.yinzhengjie.com': jasonyin2020
Password for 'http://jasonyin2020@gitlab11.yinzhengjie.com':
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 24 (delta 4), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (4/4), done.
[root@ubuntu11.yinzhengjie.com tmp]#
[root@ubuntu11.yinzhengjie.com tmp]# cd meta-data/
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git branch
* master
[root@ubuntu11.yinzhengjie.com meta-data]#
(2)使用jasonyin2020用户拉取meta-data项目的dev分支
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html # 拉取数据前,先查看首页文件内容。
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git checkout -b dev
Switched to a new branch 'dev'
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git branch
* dev
master
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git remote -v
origin http://gitlab11.yinzhengjie.com/dev/meta-data.git (fetch)
origin http://gitlab11.yinzhengjie.com/dev/meta-data.git (push)
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git pull origin dev
Username for 'http://gitlab11.yinzhengjie.com': jasonyin2020
Password for 'http://jasonyin2020@gitlab11.yinzhengjie.com':
From http://gitlab11.yinzhengjie.com/dev/meta-data
* branch dev -> FETCH_HEAD
Updating 37f7b35..19587c5
Fast-forward
.gitignore | 9 +++++++++
index.html | 4 ++++
2 files changed, 13 insertions(+)
create mode 100644 .gitignore
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
<h1>真人秀</h1>
<h1>点赞,互动</h1>
<h1>美颜,滤镜</h1>
<h1>开直播,下播</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
1.4 使用jasonyin2020用户推送数据到dev分支
(1)修改数据
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
<h1>真人秀</h1>
<h1>点赞,互动</h1>
<h1>美颜,滤镜</h1>
<h1>开直播,下播</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# echo "<h1>电商平台</h1>" >> index.html
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
<h1>真人秀</h1>
<h1>点赞,互动</h1>
<h1>美颜,滤镜</h1>
<h1>开直播,下播</h1>
<h1>电商平台</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
(2)提交数据到本地仓库
[root@ubuntu11.yinzhengjie.com meta-data]# git add .; git commit -m '电商'
[dev 99222c1] 电商
1 file changed, 1 insertion(+)
[root@ubuntu11.yinzhengjie.com meta-data]#
(3)推送代码到远程仓库
[root@ubuntu11.yinzhengjie.com meta-data]# git push origin dev
Username for 'http://gitlab11.yinzhengjie.com': jasonyin2020
Password for 'http://jasonyin2020@gitlab11.yinzhengjie.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 332 bytes | 332.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for dev, visit:
remote: http://gitlab11.yinzhengjie.com/dev/meta-data/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To http://gitlab11.yinzhengjie.com/dev/meta-data.git
19587c5..99222c1 dev -> dev
[root@ubuntu11.yinzhengjie.com meta-data]#
1.5 使用jasonyin2020用户推送数据到master分支会失败(gitlab默认的保护分支)
(1)修改数据并提交代码到本地仓库
[root@ubuntu11.yinzhengjie.com meta-data]# git branch
* dev
master
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git branch
dev
* master
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# echo "<h1>抖+投流...</h1>" >> index.html
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# cat index.html
<h1>项目成立</h1>
<h1>VR功能</h1>
<h1>VIP功能</h1>
<h1>修复Bug成功</h1>
<h1>抖+投流...</h1>
[root@ubuntu11.yinzhengjie.com meta-data]#
[root@ubuntu11.yinzhengjie.com meta-data]# git add .; git commit -m '抖加'
[master 3c59b67] 抖加
1 file changed, 1 insertion(+)
[root@ubuntu11.yinzhengjie.com meta-data]#
(2)使用jasonyin2020用户推送代码到master失败,因为该分支被保护啦~
[root@ubuntu11.yinzhengjie.com meta-data]# git push origin master
Username for 'http://gitlab11.yinzhengjie.com': jasonyin2020
Password for 'http://jasonyin2020@gitlab11.yinzhengjie.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 295 bytes | 295.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: GitLab: You are not allowed to push code to protected branches on this project.
To http://gitlab11.yinzhengjie.com/dev/meta-data.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://gitlab11.yinzhengjie.com/dev/meta-data.git'
[root@ubuntu11.yinzhengjie.com meta-data]#
2.使用管理员查看gitlab项目的默认分支保护
2.1 查看群组信息
如上图所示,使用管理员root用户登录,查看dev组的项目。
2.2 查看dev组的meta-data项目
如上图所示,查看dev组的meta-data项目。
温馨提示,不一定必须使用root用户登录,凡是对dev组的meta-date拥有Owner权限的用户均可哟~
2.3 查看默认的分支保护
如上图所示,依次点击后,就可以查看到"受保护的分支",点击"展开"按钮,就可以看到如下图所示的图片。
很明显,master分支属于默认的分支保护,即默认情况下只有Maintainer及以上的角色权限才能进行代码的合并和推送哟~
3.解决分支保护的思路
思路一:
取消分支保护,这样任意有权限访问项目的用户均可以合并代码和推送代码。
优势:
所有用户都能推送代码,开发人员操作起来比较方便。
缺点:
正式因为所有人都能推送代码到master分支,很可能某个开发写的程序未经过充分测试而在运行过程中差生bug,导致程序崩溃的现象。
思路二:
使用默认的分支保护。
优势:
只有部分拥有相应角色的用户才能推送或合并代码到master分支。起到了一定的分支保护的作用。限制了大部分用户的行为。
缺点:
需要运维或开发人员手动合并代码。
4.jasonyin2020用户发起合并代码请求
4.1 jasonyin2020用户创建合并请求
如上图所示,我们可以发起"创建合并请求"。并按照下图的方式进行提交即可。
4.2 jasonyin2020用户创建合并请求成功
如上图所示,成功创建了合并请求啦。重新刷新页面可以看到如下图所示的信息。
4.3 项目管理员root查看合并请求
如上图所示,使用meta-data管理员进行登录并查看该项目,效果如下图所示。
4.4 项目管理员root批准合并请求
如上图所示,查看到jasonyin2020用户发起的合并请求,如下图所示,点击"批准",和"合并"
5.jasonyin2020开发的代码成功合并
如上图所示,代码和合并成功啦。
如下图所示,代码合并成功发现dev分支也被删除啦!
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/18577846,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。