git

GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务
GitLab和GitHub一样属于第三方基于Git开发的作品(私有仓库),GITLAB免费且开源(基于MIT协议),与Github类似, 可以注册用户,任意提交你的代码,添加SSHKey等等。不同的是,GitLab是可以部署到自己的服务器 上,数据库等一切信息都掌握在自己手上,适合团队内部协作开发,你总不可能把团队内部的智慧总放 在别人的服务器上吧?简单来说可把GitLab看作个人版的GitHub。

1、Git
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而是一个开放源码的版本控制软件。Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必有服务器端软件支持。

安装
[root@k8s-master-01 ~]# yum install git -y

#测试是否安装成功(查看git版本号)
[root@k8s-master-01 ~]# git --version
git version 1.8.3.1

#配置用户名与邮箱
[root@k8s-master-01 ~]# git config --global user.name “yzl”
[root@k8s-master-01 ~]# git config --global user.email “1600887274@qq.com”

#查看配置信息
[root@k8s-master-01 ~]# git config -l
user.name=yzl
user.email=1600887274@qq.com

仓库分为本地仓库和远程仓库

创建本地仓库
[root@k8s-master-01 ~]# cd /mnt
[root@k8s-master-01 mnt]# mkdir test
[root@k8s-master-01 mnt]# cd test/
[root@k8s-master-01 test]# git init
初始化空的 Git 版本库于 /mnt/test/.git/
[root@k8s-master-01 test]# ll -a
总用量 0
drwxr-xr-x. 3 root root 18 8月 14 00:33 .
drwxr-xr-x. 3 root root 18 8月 14 00:32 …
drwxr-xr-x. 7 root root 119 8月 14 00:33 .git

#链接远程仓库
[root@k8s-master-01 test]# git remote add origin git@github.com:yzl-yzl/devops-test.git

#查看远程连接
方式一:
[root@k8s-master-01 test]# git remote -v
origin git@github.com:yzl-yzl/devops-test.git (fetch)
origin git@github.com:yzl-yzl/devops-test.git (push)

方式二:
[root@k8s-master-01 test]# cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “origin”]
url = git@github.com:yzl-yzl/devops-test.git
fetch = +refs/heads/:refs/remotes/origin/

#将文件添加至本地仓库
git add . #将目录中的所有文件加入仓库
git add [文件名] #将指定文件加入仓库

#删除远程链接
[root@k8s-master-01 test]# git remote rm origin

#将本地文件添加至github
[root@k8s-master-01 test]# echo “1230” > index.html
[root@k8s-master-01 test]# git add .
[root@k8s-master-01 test]# git commit -m “1230”
[root@lb01 test]# ssh-keygen
[root@k8s-master-01 test]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFGbh6kjmf92CcbLPWeTUd2KjzlNqU+ICV2uj7YDeY69ZoKNJG4jIRXNE6quVz/TYdQtL8t0ctilPMQeFm+qcIjVj4uOe8Ahj/IqRkf6yXv5Nf4H1GBww6tLgg/9BU5Gv4KQtW05tGoEqbJ2yh7n4YPus5gza/LucXJEQN3ilhaR1ssVmyq5gBAgE2ti8rqYBqjIziTQVKhL+/pEwshRw7bnKYtqiuc5DCe/laDmdJlIZVu0SybmwZrPJsF40QaCHGG7Btc1pGIHm1i0oFf61z77BsC3Y/GcFzWdZTQ44LI6Zl/uFc3IOZpmqg+MQRiQHX40oaEM3QFT5zOnm1pOrP root@k8s-master-01
在这里插入图片描述
复制上面的公钥,将公钥粘贴到
GitHub:进入官网---->点击个人头像----->点击设置----->ssh and gpg keys---->new ssh key----->将公钥粘贴即可

#将本地master分支的代码推到远程仓库
[root@k8s-master-01 test]# git push origin master
Warning: Permanently added the RSA host key for IP address ‘13.250.177.223’ to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 205 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:yzl-yzl/devops-test.git

  • [new branch] master -> master

git分支
#创建分支
[root@k8s-master-01 test]# git checkout -b test
切换到一个新分支 ‘test’

#查看当前分支
[root@k8s-master-01 test]# git branch
master

  • test

#查看所有分支
[root@k8s-master-01 test]# git branch -a
master

  • test
    remotes/origin/master

#切换分支
[root@k8s-master-01 test]# git checkout master
切换到分支 ‘master’

#查看状态
[root@lb01 test]# git status

#查看文件变化
[root@lb01 test]# git diff index.html
diff --git a/index.html b/index.html
index f70eccf…9f358a4 100644
— a/index.html
+++ b/index.html
@@ -1 +1 @@
-1230
+123456

获取代码

#当本地没有代码仓库时
git clone https://github.com/yourName/repositoryname.git

#当本地已经有了代码仓库,现在需要同步远程仓库内容
git pull origin master

拉取指定分支代码
git clone -b test

#打标签
[root@instance-gvpb80ao test]# git tag -a v1.1.5-stable -m “做了很多事”
[root@instance-gvpb80ao test]# git tag
v1.1.5-stable

#查看标签内容
[root@k8s-master-01 test]# git tag v1 -m"啥也没干"
[root@k8s-master-01 test]# git show v1
tag v1
Tagger: yzl 1600887274@qq.com
Date: Mon Aug 16 17:23:17 2021 +0800
啥也没干
commit eb24323b92f8f8f530228fe0863924096267d3f9
Author: yzl 1600887274@qq.com
Date: Mon Aug 16 17:20:46 2021 +0800
123456

diff --git a/index.htm b/index.htm
new file mode 100644
index 0000000…28d1445
— /dev/null
+++ b/index.htm
@@ -0,0 +1 @@
+123456789
#按照标签拉取代码
git clone [git链接]
git tag [tag_name]在这里插入图片描述

gitlab

安装
安装相关依赖
yum -y install policycoreutils openssh-server openssh-clients postfix
关闭防火墙
[root@instance-gvpb80ao test]# systemctl disable firewalld

#关闭selinux
sed -i ‘s#enforcing#disabled#g’ /etc/sysconfig/selinux
#临时关闭
setenforce 0

下载gitlab包,并且安装
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.0.3-ce.0.el7.x86_64.rpm

修改gitlab配置
#安装gitlab
yum install gitlab-ce-13.0.3-ce.0.el7.x86_64.rpm

重载配置及启动gitlab
#修改配置文件
[root@sean ~]# vim /etc/gitlab/gitlab.rb
external_url ‘http://192.168.12.82’
nginx[‘listen_port’] = 80

#刷新配置(默认启动)
gitlab-ctl reconfigure
#启动
gitlab-ctl start

添加gitlab邮件

#编辑/etc/gitlab/gitlab.rb
把下列代码加入Email account username

tlab_rails['smtp_enable']=true
gitlab_rails['smtp_address']="smtp.qq.com"
gitlab_rails['smtp_port']=465
gitlab_rails['smtp_user_name']="1600887274@qq.com"
gitlab_rails['smtp_password']="dhkgdhezyqxriahc"# 
gitlab_rails['smtp_domain']="smtp.qq.com"
gitlab_rails['smtp_authentication']="login"
gitlab_rails['smtp_enable_starttls_auto']=true
gitlab_rails['smtp_tls']=true
gitlab_rails['gitlab_email_from']='1600887274@qq.com'
gitlab_rails['gitlab_email_enabled']=true

#配置释义
gitlab_rails['smtp_enable']=true				# 启用邮件服务
gitlab_rails['smtp_address']="smtp.qq.com"		# 指定发送邮件的服务
gitlab_rails['smtp_port']=465					# 发送邮件服务的端口号
gitlab_rails['smtp_user_name']="alvincy@qq.com"	# 发送邮件的邮箱
gitlab_rails['smtp_password']="rclugdxvchlxjddg" # 授权码
gitlab_rails['smtp_domain']="smtp.qq.com"		# 发送油价的邮箱
gitlab_rails['smtp_authentication']="login"		# 登录事件
gitlab_rails['smtp_enable_starttls_auto']=true   #是否启用ttl
gitlab_rails['smtp_tls']=true      是否启用tls(https)
gitlab_rails['gitlab_email_from']='alvincy@qq.com'   # 发送邮件的邮箱
gitlab_rails['gitlab_email_enabled']=true   # 是否启用发送邮件

刷新配置并重启
gitlab-ctl reconfigure
gitlab-ctl restart

gitlab添加组、创建用户、创建项目

创建组
使用管理员 root 创建组,一个组里面可以有多个项目分支,可以将开发添加到组里面进行设置权限, 不同的组就是公司不同的开发项目或者服务模块,不同的组添加不同的开发即可实现对开发设置权限的管理。
在这里插入图片描述
在这里插入图片描述

创建用户

在这里插入图片描述
在这里插入图片描述
将用户绑定用户组
在这里插入图片描述
用户有5种角色
1.Guest:可以创建issue、发表评论,不能读写版本库
2.Reporter:可以克隆代码,不能提交,QA、PM 可以赋予这个权限
3.Developer:可以克隆代码、开发、提交、push,普通开发可以赋予这个权限
4.Maintainer:可以创建项目、添加tag、保护分支、添加项目成员、编辑项目,核心开发可以赋予这个权限
5.Owner:可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员,开发组组长可以赋予这个权限

创建项目

在这里插入图片描述
gitlab的标签
新建标签:点击项目下边的标签---->新建标签---->填写标签的具体信息即可
标签可以帮助我们更好的识别代码的版本,便于管理,同时标签只能增加或删除,不能修改。

标签的本质就是一个特殊的分支,它可以作为分支来用
示例:
当前位于v2-test

更改文件内容:原文内容为123
保存后克隆

[root@k8s-master-01 test]# ls
index.htm  index.html
[root@k8s-master-01 test]# git clone git@192.168.13.11:yzl/html.git
正克隆到 'html'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
接收对象中: 100% (9/9), done.
[root@k8s-master-01 test]# cat index.html
123
[root@k8s-master-01 test]# cat index.htm
123456789
[root@k8s-master-01 test]# ls
html  index.htm  index.html
[root@k8s-master-01 test]# cd html/
[root@k8s-master-01 html]# ls
html  README.md
#查看文件内容
[root@k8s-master-01 html]# cat html 
123
123
123
23
1321
321
312
31
3
1312
[root@k8s-master-01 html]# 

切换标签(分支)

[root@k8s-master-01 html]# git checkout v2-test
Note: checking out 'v1-test'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
  git checkout -b new_branch_name
HEAD 目前位于 17a9fcd... Initial commit

[root@k8s-master-01 html]# ls
html  README.md
[root@k8s-master-01 html]# cat html 
123

发现内容改变

克隆指定分支,修改文件后并将修改上传至指定分支

git clone -b test git@192.168.1.230:yuzhilong/test.git
cd test/

增删改文件

git add .
git commit -am “456”
git push

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐