GitLab基本使用

基础设置

  • 修改登录界面外观

  • 关闭自动注册功能

设置好,保存即可


创建用户及用户组

  • 创建用户组

  • 创建用户

  • 给用户加到用户组里,并给与相关权限


创建项目

  • 项目创建

  • 给gitlab添加ssh秘钥


上传和拉取代码

需要注意,上传代码之前,需要将服务器公钥添加到gitlab上,否则会上传失败

  • 本地上传代码
[root@git ~]# cd /git_test/
[root@git git_test]# git init
Initialized empty Git repository in /git_test/.git/
[root@git git_test]# touch {1..3}.txt
[root@git git_test]# ll
total 0
-rw-r--r-- 1 root root 0 May 13 00:23 1.txt
-rw-r--r-- 1 root root 0 May 13 00:23 2.txt
-rw-r--r-- 1 root root 0 May 13 00:23 3.txt
[root@git git_test]# git add .
[root@git git_test]# git commit -m "add new file"
[master (root-commit) 0657332] add new file
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt
 create mode 100644 2.txt
 create mode 100644 3.txt
[root@git git_test]# git remote add origin git@10.0.0.91:Dev/test.git
[root@git git_test]# git push -u origin master 
The authenticity of host '10.0.0.91 (10.0.0.91)' can't be established.
ECDSA key fingerprint is SHA256:G8YGd5X1RZ/Uj+7a0C6vJpamET/dKNGZvpbDkZCJG2U.
ECDSA key fingerprint is MD5:90:56:46:12:e7:0e:dc:42:06:3a:d8:cf:6a:5f:22:8e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.91' (ECDSA) to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.91:Dev/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[root@git git_test]#

上传成功后,gitlab远程仓库会显示出来

  • 本地修改文件,更新代码
[root@git git_test]# echo "111111" > 1.txt 
[root@git git_test]# git add .
[root@git git_test]# git commit -m "update 1.txt" 
[master d62a740] update 1.txt
 1 file changed, 1 insertion(+)
[root@git git_test]# git push -u origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.91:Dev/test.git
   0657332..d62a740  master -> master
Branch master set up to track remote branch master from origin.
[root@git git_test]# 

  • 克隆代码到本地
[root@git ~]# cd /tmp/
[root@git tmp]# git clone git@10.0.0.91:Dev/test.git
Cloning into 'test'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
[root@git tmp]# ll
total 0
drwxr-xr-x 3 root root 57 May 13 00:36 test
[root@git tmp]# cd test/
[root@git test]# ll
total 4
-rw-r--r-- 1 root root 7 May 13 00:36 1.txt
-rw-r--r-- 1 root root 0 May 13 00:36 2.txt
-rw-r--r-- 1 root root 0 May 13 00:36 3.txt
[root@git test]# 
  • 普通用户开发人员上传代码

另外准备一台开发人员专用服务器,并生成ssh秘钥,把公钥添加到Gitlab上

[root@dev ~]# git clone git@10.0.0.91:Dev/test.git
Cloning into 'test'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
[root@dev ~]# ll
total 0
drwxr-xr-x 3 root root 57 May 13 00:48 test
[root@dev ~]# cd test/
[root@dev test]# ll
total 4
-rw-r--r-- 1 root root 7 May 13 00:48 1.txt
-rw-r--r-- 1 root root 0 May 13 00:48 2.txt
-rw-r--r-- 1 root root 0 May 13 00:48 3.txt
[root@dev test]# touch {a,b,c}-dev01.txt
[root@dev test]# git add .
[root@dev test]# git commit -m "add new files dev01"
[master b312434] add new files dev01
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a-dev01.txt
 create mode 100644 b-dev01.txt
 create mode 100644 c-dev01.txt
[root@dev test]# git push -u origin master
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@10.0.0.91:Dev/test.git
   d62a740..b312434  master -> master
Branch master set up to track remote branch master from origin.
[root@dev test]# 

上传成功后,刷新gitlab远程仓库,可见新代码已经上传


分支保护

需要使用管理员账号才能设置分支保护

  • 使用普通用户上传代码到master分支失败
[root@dev test]# git pull git@10.0.0.91:Dev/test.git
From 10.0.0.91:Dev/test
 * branch            HEAD       -> FETCH_HEAD
Already up-to-date.
[root@dev test]# ll
total 8
-rw-r--r-- 1 root root 7 May 13 00:48 1.txt
-rw-r--r-- 1 root root 7 May 13 00:59 2.txt
-rw-r--r-- 1 root root 0 May 13 00:48 3.txt
-rw-r--r-- 1 root root 0 May 13 00:49 a-dev01.txt
-rw-r--r-- 1 root root 0 May 13 00:49 b-dev01.txt
-rw-r--r-- 1 root root 0 May 13 00:49 c-dev01.txt
[root@dev test]# touch d-dev01.txt
[root@dev test]# git add .
[root@dev test]# git commit -m "add d-dev01.txt dev01"
[master 8c5b423] add d-dev01.txt dev01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d-dev01.txt
[root@dev test]# git push -u origin master 
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 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 git@10.0.0.91:Dev/test.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@10.0.0.91:Dev/test.git'
[root@dev test]# 

可以看到,push代码的时候出现了报错。原因是我们做了分支保护,master分支只有管理员用户才能上传代码

  • 切换到普通分支,以普通用户推送代码到远程仓库的普通分支成功
[root@dev test]# touch test01.txt
[root@dev test]# git add .
[root@dev test]# git commit -m "add test01.txt"
[test01 c76afa5] add test01.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test01.txt
[root@dev test]# git push -u origin test01 
Counting objects: 16, done.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (16/16), 1.24 KiB | 0 bytes/s, done.
Total 16 (delta 5), reused 4 (delta 0)
remote: 
remote: To create a merge request for test01, visit:
remote:   http://10.0.0.91/Dev/test/merge_requests/new?merge_request%5Bsource_branch%5D=test01
remote: 
To git@10.0.0.91:Dev/test.git
 * [new branch]      test01 -> test01
Branch test01 set up to track remote branch test01 from origin.
[root@dev test]# 
  • 使用管理员用户上传代码到master分支,成功
[root@git test]# git pull git@10.0.0.91:Dev/test.git
From 10.0.0.91:Dev/test
 * branch            HEAD       -> FETCH_HEAD
Already up-to-date.
[root@git test]# touch master.txt
[root@git test]# git add .
[root@git test]# git commit -m "add master.txt"
[master c688c09] add master.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.txt
[root@git test]# git push -u origin master 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 355 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@10.0.0.91:Dev/test.git
   7445abf..c688c09  master -> master
Branch master set up to track remote branch master from origin.
[root@git test]# 

合并分支

把普通分支(test01)合并到主分支(master)上

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注