Gitlab warehouse management settings protection branch

 

 

Setting protection branch

The opinion, which branch did not want to let others move, put the branch to branch protection, and only authorized users can push this to the branch code

 

In actual use, we usually keep the master branch stable production environment for the release version, only authorized users can merge code to master.

To achieve this, we need to master to protect the branches, and authorized what the user can push the code to the master user.


Use root user clicks on the lower left corner of the Settings page git_test warehouse

 

 

 Enter the settings page, select Settings Repository below the menu bar options

 

 

 

 Into the repository settings page

 

 

 

Expand Protected Branches 

 

 

 

After the setup is complete, the warehouse branch page, you can see the emergence of a green protected mark behind the master branch. Means that the master branch to be protected

 

 

 

 

 

Only master roles can apply to merge the master branch, we can push the code to the master branch to master branch operation, only the master can do

admin is the master identity

dev does not have permission to push to push the master branch code, you do not have permission to apply for master merge

 

 

 

 

ci-node1 corresponding to master

ci-node2 user corresponds dev

 

At this point we try to push the master branch on the ci-node2 to GitLab

 In the master branch switch to branch dev

 

[root@ci-node2 git_test]# git branch
* dev
  master

[root@ci-node2 git_test]# git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'

 

Create a new file, then submit, push the ci-node2 client

 

 

[root@ci-node2 git_test]# touch ci-node2
[root@ci-node2 git_test]# git add .
[root@ci-node2 git_test]# git commit -m "commit ci-node2 on ci-node2"
[master 2bd2e88] commit ci-node2 on ci-node2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ci-node2

An error 

We found that at this time we can not push to the master branch on GitLab on ci-node2, because we are bound ci-node2 user dev, dev user belongs to the developer role,

master developer branch is not allowed to push content to their roles.

[root@ci-node2 git_test]# git push -u origin master
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 234 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To 192.168.31.11:test/git_test.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:test/git_test.git

 

 

Example 2

In the dev branch

[root@ci-node2 git_test]# git branch
* dev
  master

 

[root@ci-node2 git_test]# ll
total 4
-rw-r--r-- 1 root root 21 Mar 30 00:11 a
-rw-r--r-- 1 root root  0 Mar 30 00:34 dev
-rw-r--r-- 1 root root  0 Mar 30 00:11 master
-rw-r--r-- 1 root root  0 Mar 30 00:11 test

 

Create a file and then submit

[root@ci-node2 git_test]# touch ci-node2
[root@ci
-node2 git_test]# ll total 4 -rw-r--r-- 1 root root 21 Mar 30 00:11 a -rw-r--r-- 1 root root 0 Apr 1 23:05 ci-node2 -rw-r--r-- 1 root root 0 Mar 30 00:34 dev -rw-r--r-- 1 root root 0 Mar 30 00:11 master -rw-r--r-- 1 root root 0 Mar 30 00:11 test

[root@ci
-node2 git_test]# git add . [root@ci-node2 git_test]# git commit -m "touch ci-node2 file on dev branch" [dev be5cdbe] touch ci-node2 file on dev branch 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ci-node2

Dev pushed to the user

 

[root@ci-node2 git_test]# git push -u origin dev
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 247 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.31.11/test/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To 192.168.31.11:test/git_test.git
   a0e7b8d..be5cdbe  dev -> dev
Branch dev set up to track remote branch dev from origin.

 

Push up the

 

Switch to the master branch

 

[root@ci-node2 git_test]# git checkout master
Switched to branch 'master'

Dev branches merged into the master branch

[root@ci-node2 git_test]# git merge dev
Updating cc7da0e..be5cdbe
Fast-forward
 ci-node2 | 0
 dev      | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ci-node2
 create mode 100644 dev

Push the master branch

Error because the ci-node2 public key bindings on gitlab dev users, set users on dev gitlab a developer, the developer does not have permission to push to the master

[root@ci-node2 git_test]# git push -u origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To 192.168.31.11:test/git_test.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:test/git_test.git'

 

Push back on the master branch ci-node1

 

[root@ci-node1 git_test]# git push -u gitlab master
Branch master set up to track remote branch master from gitlab.
Everything up-to-date

ci-node2 上做了修改 推送了仓库内容,ci-node1现在本地仓库和远程仓库 内容 不一致,导致以上情况

 

这时候需要 用git fetch命令

 

 

git fetch 使用

上面我们在 ci-node2 向 gitlab 上的远程仓库推送了新的内容,此时对于 ci-node1 上的 git_test 仓库来说,它的远程仓库已经更新,所以需要将这些更新取回本地,这时就需 要用到 git fetch 命令。

fetch到本地,然后合并,再推送

 

 

[root@ci-node1 git_test]# git fetch
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From 192.168.31.11:test/git_test
 * [new branch]      dev        -> gitlab/dev

 

合并   把更新内容合并到本地

[root@ci-node1 git_test]# git merge gitlab/dev
Updating cc7da0e..be5cdbe
Fast-forward
 ci-node2 | 0
 dev      | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ci-node2
 create mode 100644 dev

有ci-node2文件

 

[root@ci-node1 git_test]# ll
total 4
-rw-r--r-- 1 root root 21 Mar 21 23:53 a
-rw-r--r-- 1 root root  0 Apr  2 23:49 ci-node2
-rw-r--r-- 1 root root  0 Apr  2 23:49 dev
-rw-r--r-- 1 root root  0 Mar 21 18:31 master
-rw-r--r-- 1 root root  0 Mar 18 00:20 test

 

再推送 

推成功了

 

[root@ci-node1 git_test]# git push -u gitlab master
Total 0 (delta 0), reused 0 (delta 0)
To 192.168.31.11:test/git_test.git
   cc7da0e..be5cdbe  master -> master
Branch master set up to track remote branch master from gitlab.

 

再看看gitlab 有ci-node2文件

 现在ci-node1 客户端上 对ci-node2 文件追加内容 ,提交

[root@ci-node1 git_test]# echo "222" >> ci-node2 

[root@ci-node1 git_test]# git add .
[root@ci-node1 git_test]# git commit -m "modify ci-node2 file on master branch"
[master b5692cb] modify ci-node2 file on master branch
 1 file changed, 1 insertion(+)

再推送

[root@ci-node1 git_test]# git push -u gitlab master
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.31.11:test/git_test.git
   be5cdbe..b5692cb  master -> master
Branch master set up to track remote branch master from gitlab

推送成功再去看看 gitlab,推上来了,dev用户推不上来,ci-node1 可以推上来,ci-node1 公钥绑定的是root用户 admin用户,

ci-node2 公钥绑定要dev用户,我们设置master分支只有master角色才能推送

 

 

 不让dev用户推到master分支,dev用户只能推到dev分支上,做完还可以提个申请,申请把代码合并到master

 

 

切换到dev分支

 

[root@ci-node2 git_test]# git branch
  dev
* master
[root@ci-node2 git_test]# git checkout dev
Switched to branch 'dev'
Your branch is up-to-date with 'origin/dev'.

 

 

 

 

我们创建一个文件 ci-node2 然后提交 推送到gitlab 上的dev用户

 

[root@ci-node2 git_test]# touch ci-node2
[root@ci-node2 git_test]# ll
total 4
-rw-r--r-- 1 root root 21 Mar 30 00:11 a
-rw-r--r-- 1 root root  0 Apr  4 18:36 ci-node2
-rw-r--r-- 1 root root  0 Mar 30 00:34 dev
-rw-r--r-- 1 root root  0 Mar 30 00:11 master
-rw-r--r-- 1 root root  0 Mar 30 00:11 test

[root@ci-node2 git_test]# git add .
[root@ci-node2 git_test]# git commit -m "touch ci-node2 file on dev branch"
[dev 30de0f6] touch ci-node2 file on dev branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 ci-node2

 

 

[root@ci-node2 git_test]# git remote
origin
[root@ci
-node2 git_test]# git push -u origin dev Counting objects: 2, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 247 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: remote: To create a merge request for dev, visit: remote: http://192.168.31.11/test/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev remote: To 192.168.31.11:test/git_test.git a0e7b8d..30de0f6 dev -> dev Branch dev set up to track remote branch dev from origin.

 

 

 

在gitlab登录dev用户 在dev分支上做了改动  ,然后可以在web界面提交合并申请 把dev分支合并到master分支

dev分支上收到推送的ci-node2文件

 

 

 

 

出现界面填信息

 

 

 

 

然后退出来,登录root用户 发现合并请求

 

 

 

 

 

 

 

 这里可以看合并了什么东西 有什么改变

 

 没有问题就merge

 

 

点完merge以后,他会告诉你 已经合并过来了

 

 再看看 master分支的仓库 ci-node2 合并进来master

 

 没有权限推送到master,你可以在提合并申请,合并申请有合并内容,

Guess you like

Origin www.cnblogs.com/mingerlcm/p/12602425.html