一、创建用户

1.填写用户信息
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pyMSD8uR-1650354069518)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183234249.png)]](https://i-blog.csdnimg.cn/blog_migrate/45d644039c61c4eb9428ff0b750e885e.png)
2.分配权限
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CO4qN9IL-1650354069519)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183332332.png)]](https://i-blog.csdnimg.cn/blog_migrate/4d6b3c292eacaafd085592d0e71c695b.png)
3.修改用户初始密码
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DqSML8cZ-1650354069520)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183600916.png)]](https://i-blog.csdnimg.cn/blog_migrate/7056dc7c48e8569c2ddd091c9828c88c.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ODzOesnf-1650354069520)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183628845.png)]](https://i-blog.csdnimg.cn/blog_migrate/a98e89da05cb2e0b9f2841aa02a0f2a9.png)
然后保存修改
二、将用户添加到指定的项目组
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T0TLOhvk-1650354069521)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418183938365.png)]](https://i-blog.csdnimg.cn/blog_migrate/b0bff2763fdd80a256a73d8b1b7db712.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jR5zE0u9-1650354069522)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418184025415.png)]](https://i-blog.csdnimg.cn/blog_migrate/c66d10124ea4991527787f1b7631b64a.png)
这里就看到我们添加的张三用户了
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MyjfSlBs-1650354069522)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220418184139374.png)]](https://i-blog.csdnimg.cn/blog_migrate/15df2fd09d071b63ddcea505db3041b0.png)
1.使用张三的身份克隆代码
[root@node-2 data]# git clone http://zhangsan@pangbing.com/root/taikang_test.git
Cloning into 'taikang_test'...
Password for 'http://zhangsan@pangbing.com':
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
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 zhangsanv1
2.提交代码
[root@node-2 taikang_test]# git push origin master
Password for 'http://zhangsan@pangbing.com':
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://zhangsan@pangbing.com/root/taikang_test.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://zhangsan@pangbing.com/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)]](https://i-blog.csdnimg.cn/blog_migrate/92dd17eb4899ea27e5a66c7f85a6b21f.png)
完全保护:开发人员不能推送提交,只有主程序员可以。不允许任何人强制推送或删除分支
2.2 项目分支保护功能
在每一个项目中都可以设置属于自己的项目的分支保护
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HsR85cNJ-1650354069525)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419131043044.png)]](https://i-blog.csdnimg.cn/blog_migrate/c37d15ba4694d616e36fbd29dc3db85c.png)
由此可以看出上边提交代码错误,是因为zhangsan是开发者,没有权限提交到gitlab代码库
3.修改用户提交权限
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wFi39BYv-1650354069526)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419131836216.png)]](https://i-blog.csdnimg.cn/blog_migrate/a28c6d1c53899b134a1e0b56cd6c5ce1.png)
这样就开发者也就可以提交到master分支了。或者把zhangsan直接变成
[root@node-2 taikang_test]# git add -A
[root@node-2 taikang_test]# git commit -m 'zhangsanv1'
[master 3a22927] zhangsanv1
1 file changed, 1 insertion(+)
[root@node-2 taikang_test]# git push origin master
Password for 'http://zhangsan@pangbing.com':
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://zhangsan@pangbing.com/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)]](https://i-blog.csdnimg.cn/blog_migrate/a8ca12c49d8754a1ec0343ddc25f5f94.png)
四、复现生产分支保护设置
1.新建分支
图1: ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yGnQZKEr-1650354069529)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133508536.png)]](https://i-blog.csdnimg.cn/blog_migrate/4a1dcc0edc49fc4eb7428d5c617f255e.png)
图2:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GQACLAc9-1650354069530)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133422117.png)]](https://i-blog.csdnimg.cn/blog_migrate/19e9093891ac14f73472d336f25316ec.png)
图3:分支已经创建完成
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JJo3FoIh-1650354069531)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133604485.png)]](https://i-blog.csdnimg.cn/blog_migrate/c4229cb8c2feb6762e2bc68714e3c318.png)
2.分支上新建文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NA2IUTyj-1650354069532)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419133834215.png)]](https://i-blog.csdnimg.cn/blog_migrate/5309e87b973fb61dbdafe8130c1e3570.png)
3.客户端克隆仓库
#以zhangsan用户的身份克隆仓库
[root@node-2 data]# git clone http://zhangsan@pangbing.com/root/taikang_test.git
Cloning into 'taikang_test'...
Password for 'http://zhangsan@pangbing.com':
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
1 file changed, 1 insertion(+)
create mode 100644 test01
[root@node-2 taikang_test]# git push origin test-500
Password for 'http://zhangsan@pangbing.com':
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://zhangsan@pangbing.com/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)]](https://i-blog.csdnimg.cn/blog_migrate/03e0a696458051d4fdb3391d7ed759ab.png)
图2:
在这里发现 系统会自动提示在taikang_test项目中有可以合并分支的请求
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mff6HMe9-1650354069534)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419144518190.png)]](https://i-blog.csdnimg.cn/blog_migrate/db25b108d8428535c77ee75cd26bfb84.png)
当然点击项目进入项目主页之后也会看到 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wIdKaNbq-1650354069535)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152027022.png)]](https://i-blog.csdnimg.cn/blog_migrate/fab753287b8ff6c17d09fe19416c367f.png)
5.发送合并代码请求
图1: ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QCXynoxt-1650354069537)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152122985.png)]](https://i-blog.csdnimg.cn/blog_migrate/957c773feddf9a0aa53be13c88c64db7.png)
图2:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FgqUKGoR-1650354069538)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152206112.png)]](https://i-blog.csdnimg.cn/blog_migrate/7ebc07ab50474023e36b5657ee7917a5.png)
6.使用管理员登录gitlab 处理合并请求

图2:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FkOSsz0f-1650354069540)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419152955265.png)]](https://i-blog.csdnimg.cn/blog_migrate/85164788e50cdce89bea0a77f9002e3c.png)
图3:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EmbeMH9D-1650354069541)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419153016384.png)]](https://i-blog.csdnimg.cn/blog_migrate/1d2ef53fd04f4bf7cd4c9e29f5800526.png)
图4:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JempBlFm-1650354069542)(gitlab%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE.assets/image-20220419153057031.png)]](https://i-blog.csdnimg.cn/blog_migrate/0831bdf19a63d223d11d6926478b75ac.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)]](https://i-blog.csdnimg.cn/blog_migrate/701719b0478849ddd123260a8983886e.png)
本文详细介绍了如何在GitLab中创建用户、分配权限,并针对代码提交错误分析了分支保护机制。通过设置全局和项目分支保护,确保开发者的提交权限。在实际生产环境中,通常会设置test分支供开发和主程序员共同使用,而master分支仅主程序员可推送。此外,演示了创建新分支、提交代码、发起合并请求以及管理员处理合并请求的流程。
6029

被折叠的 条评论
为什么被折叠?



