4-Gitlab使用项目

4-Gitlab使用项目

找两台虚拟机作为开发机

我这里找来两台centos7.6的系统,其他的系统也可以

IP:10.0.0.10,10.0.0.11

系统:Centos7.6

利用李四用户,test1这个项目,基于http把项目下载到centos7.6-1这台开发机器上面

image-20220719135456205.png


这是李四的电脑克隆test1这个项目10.0.0.10

这是克隆地址:http://gitlab.9527edu.org/9527edu/test1.git


创建一个目录作为项目的目录

[root@centos7 ~]# mkdir -p /dada/test1


进入项目的目录里面

[root@centos7 ~]# cd /dada/test1


修改本地的hosts解析,不然利用域名解析不到这个域名

vim /etc/hosts
最后一行添加如下内容
10.0.0.80 gitlab.9527edu.org


克隆test1这个项目到本地,会要求你添加开发者的用户名和密码,这里是lisi密码123456789

如果没有git可以先安装
[root@centos7 test1]# yum install git -y
[root@centos7 test1]# git clone http://gitlab.9527edu.org/9527edu/test1.git
Cloning into 'test1'...
Username for 'http://gitlab.9527edu.org': lisi
Password for 'http://lisi@gitlab.9527edu.org': 123456789(密码默认不显示)
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.


进入到项目test1里去,默认路径在/data/test1/

[root@centos7 test1]# cd test1/


查看里面有哪些文件

[root@centos7 test1]# ls
README.md


查看README.md

[root@centos7 test1]# cat README.md
# test1


再到张三的机器上面操作如上操作10.0.0.11

创建一个目录作为项目的目录

[root@centos7 ~]# mkdir -p /dada/test1


进入项目的目录里面

[root@centos7 ~]# cd /dada/test1


修改本地的hosts解析,不然利用域名解析不到这个域名

vim /etc/hosts
最后一行添加如下内容
10.0.0.80 gitlab.9527edu.org


克隆test1这个项目到本地,会要求你添加开发者的用户名和密码,这里是zhangsan密码123456789

如果没有git可以先安装
[root@centos7 test1]# yum install git -y
[root@centos7 test1]# git clone http://gitlab.9527edu.org/9527edu/test1.git
Cloning into 'test1'...
Username for 'http://gitlab.9527edu.org': zhangsan
Password for 'http://lisi@gitlab.9527edu.org': 123456789(密码默认不显示)
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.


进入到项目test1里去,默认路径在/data/test1/

[root@centos7 test1]# cd test1/


查看里面有哪些文件

[root@centos7 test1]# ls
README.md


查看README.md

[root@centos7 test1]# cat README.md
# test1


张三开发人员添加解释

[root@centos7 test1]# vim README.md
# test1 READme
# data 2022/07/19-14


利用git把项目文件上传到服务器上面

[root@centos7 test1]# git add .;git commit -m "modify REAMDE"
[main bb72cf7] modify REAMDE
 Committer: root <root@centos7.6-2>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+), 1 deletion(-)


添加git用户和邮箱

[root@centos7 test1]# git config --global user.name zhangsan
[root@centos7 test1]# git config --global user.mail 1454544245@qq.com


把写好的文件上传到服务器上面,需要输入zhangsan和123456789

[root@centos7 test1]# git push 
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://gitlab.9527edu.org': zhangsan
Password for 'http://zhangsan@gitlab.9527edu.org': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://gitlab.9527edu.org/9527edu/test1.git
   809bd42..bb72cf7  main -> main


然后在浏览器里面查看README.md这个文件有没有修改

image-20220719144808207.png

因为文件已经做修改了,李四(10.0.0.10)需要重新把最新的代码拉取下来,需要输入lisi和123456789

[root@centos7 test1]# git pull
Username for 'http://gitlab.9527edu.org': lisi
Password for 'http://lisi@gitlab.9527edu.org': 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From http://gitlab.9527edu.org/9527edu/test1
   809bd42..bb72cf7  main       -> origin/main
Updating 809bd42..bb72cf7
Fast-forward
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)


查看一下README.md文件

[root@centos7 test1]# cat README.md
# test1 README
# data 2022/07/19-14


因为李四是开发人员,需要提交代码,编写一个文件

[root@centos7 test1]# vim index.html
写入一下内容
<h1> test1 v1.0 </h>


把刚才写好的文件上传到服务器上面去

添加上传者信息
[root@centos7 test1]# git config --global user.name lisi
[root@centos7 test1]# git config --global user.mail 3513075301@qq.com
添加到暂存区
[root@centos7 test1]# git add .;git commit -m "add index.html v1.0"
[main 4e97469] add index.html v1.0
 Committer: lisi <root@centos7.6-1>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 index.html
 上传index.html
 [root@centos7 test1]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://gitlab.9527edu.org': lisi
Password for 'http://lisi@gitlab.9527edu.org': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.To http://gitlab.9527edu.org/9527edu/test1.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'http://gitlab.9527edu.org/9527edu/test1.git'

报错

remote: GitLab: You are not allowed to push code to protected branches on this project.To http://gitlab.9527edu.org/9527edu/test1.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'http://gitlab.9527edu.org/9527edu/test1.git'
解释:
	Developer禁止上传非保护分支Proteced branches


图中的main是保护分支

image-20220719150632686.png


保护分支,用管理员用户登录gitlab

找到test1的项目,把允许合并改成devel和

image-20220719151211343.png


image-20220719152927443.png


写入代码,把原来的1.0修改成2.0

[root@centos7 test1]# vim index.html
<h1> test1 v2.0 </h>


把index.html添加到缓存区

[root@centos7 test1]# git add .;git commit -m " up v2.0"
[dev 3d8e995]  up v2.0
1 file changed, 1 insertion(+), 1 deletion(-)


把代码推送到服务器上面

[root@centos7 test1]# git push


查看

image-20220719153719403.png

张三这台机器上面创建一个dev的分支

git切换分支
[root@centos7 test1]#  git checkout 分支name
创建dev分支
[root@centos7 test1]# git checkout -b dev

查看当前在哪个分支
[root@centos7 test1]# git branch

添加index.html v3.0
[root@centos7 test1]# vim index.html
内容:
<h1> test1 v3.0 </h1>

添加到缓存区并添加注释
[root@centos7 test1]# git add .;git commit -m "up v3.0"

上传到服务器上面
[root@centos7 test1]# git push --set-upstream origin dev
Username for 'http://gitlab.9527edu.org': zhangsan
Password for 'http://zhangsan@gitlab.9527edu.org': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://gitlab.9527edu.org/9527edu/test1/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://gitlab.9527edu.org/9527edu/test1.git
   bb72cf7..da53386  dev -> dev
Branch dev set up to track remote branch dev from origin.


查看dev分支

image-20220719155516876.png


每次拉取上传都需要输入账号密码很麻烦,基于key验证做验证

生成密钥
[root@centos7 test1]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wIxtBKWXRUHYGXA7ewPySlIor2ObzPebit3DwZUz3hY root@centos7.6-1
The key's randomart image is:
+---[RSA 2048]----+
|    .o+*B+       |
|     O.+o.       |
|  . + @ +.       |
|   o + +=+E      |
|    o..oS+o.     |
|   . oo...o.     |
|  +  ... .       |
| + *..o.         |
|  *.oo=o         |
+----[SHA256]-----+

查看
[root@centos7 test1]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPCpPzC/kKErz7TOGRpqexErT7VHnChIP4lzcv5MJpcPYz+YXVEFJwomRbD5phJN0qMVe+aiyzdhdWIOwirMormlRc3IHA4hTnOlOmSz2Cy9u5uJWew2EySB+RR8+o1ZiIM1Iki6ncdR3V9V/5sY6jr9iTlQ4sNlhMAUvdbBlyDaA8fWL38tRx0gU9Pu9amKmlIhm1DpsdatIVhFCjRcrCxVwZgFqhhM8IRDGzayE5rrfsfhpVBF8q4E/fyAgcxrtGF9RA2XK5mQIS63ohfe1W/vprs/jP6oZd8FIu2HAeSNqdxK/CgsIxeh9qcsOA+LIKsO4dyEY+Cv6Uq92Lq0ZF root@centos7.6-1


用李四的账号登录gitlab


image-20220719170535181.png


粘贴好刚才查看到的key,复制的对话框里面

image-20220719170728992.png


image-20220719170840625.png


测试一下,把版本改成3.0

[root@centos7 test1]# vim index.html
<h1> test1 v3.0 </h>


因为默认走的是http,需要改成ssh

[root@centos7 test1]# git remote -v
origin	http://gitlab.9527edu.org/9527edu/test1.git (fetch)
origin	http://gitlab.9527edu.org/9527edu/test1.git (push


把之前的关联信息移除掉

[root@centos7 test1]# git remote remove origin


重新建立连接关系,基于ssh的拉取

[root@centos7 test1]# git remote add origin git@gitlab.9527edu.org:9527edu/test1.git


再来上传

[root@centos7 test1]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

The authenticity of host 'gitlab.9527edu.org (10.0.0.80)' can't be established.
ECDSA key fingerprint is SHA256:dn/Gp16ARcpXlYRcZ+cqE0TaGvjgcmzJDsCT5BL5uSw.
ECDSA key fingerprint is MD5:b2:52:37:a1:64:f3:dd:9b:8c:d0:cd:2c:75:85:ae:c5.
Are you sure you want to continue connecting (yes/no)? yes 这里需要敲yes
Warning: Permanently added 'gitlab.9527edu.org,10.0.0.80' (ECDSA) to the list of known hosts.
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 432 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@gitlab.9527edu.org:9527edu/test1.git
   5537bc1..17663ce  main -> main
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@gitlab.9527edu.org:9527edu/test1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


查看


image-20220719174051866.png


升级dev的分支index.html文件为v4.0

[root@centos7 test1]# vim index.html
<h1> test1 v4.0 </h1>
[root@centos7 test1]# git add .;git commit -m "up v4.0"
[dev a3d1ef9] up v4.0
1 file changed, 1 insertion(+), 1 deletion(-)
[root@centos7 test1]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://gitlab.9527edu.org': lisi
Password for 'http://lisi@gitlab.9527edu.org': 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: View merge request for dev:
remote:   http://gitlab.9527edu.org/9527edu/test1/-/merge_requests/1
remote: 
To http://gitlab.9527edu.org/9527edu/test1.git
   da53386..a3d1ef9  dev -> dev
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'http://gitlab.9527edu.org/9527edu/test1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

开发分支和main分支,开发分支合并到main分支

image-20220720085054249.png


image-20220720085119063.png


可以用李四的账号发起请求

image-20220720085251921.png


image-20220720091824552.png


这里的合并只是提交申请,需要有人同意

image-20220720090110259.png


批准请求一般是由管理人员做批准,这里的账号是管理员或者张三组管理员做批准请求

image-20220720092044761.png


image-20220720092100772.png


解决合并问题

image-20220720092222305.png


因为我们是4.0的所有选择4.0的

image-20220720092253635.png


提交到源分支

image-20220720092336034.png


image-20220720092459390.png

本文 暂无 评论

Top