目录

一、Git、Github、Gitlab 的区别

二:Git介绍

1、Git 与 SVN 区别

2、Git工作流程

三、git安装

1、yum安装

2.Windows 平台上安装

3、Mac 平台上安装

四、git使用

1、Git 用户信息

2、查看配置信息

3、Git 使用

(1)从github克隆文件到本地服务器(首先github中需要提前存在文件/仓库)

(2)从本地服务器上传文件到远程github仓库

(3)分支管理

(4)版本回退

(5)注意

五、安装本地 Git 服务器

六、Gitlab server部署

1、安装Gitlab

2、使用Gitlab

1、Gitlab 服务管理

2、登陆Gitlab

六、Gitlab 设置 HTTPS 方式

1、创建私有密钥

2、创建私有证书

3、创建CRT签署证书

4、创建pem证书

5、查看生成的证书

6、更改文件权限

7、配置gitlab

8、初始化gitlab相关服务配置

9、nginx配置

10、重启gitlab

11、浏览器登录gitlab

七、Gitlab添加SMTP邮件功能

1、配置邮件功能

2、停止gitlab服务

3、修改配置后初始化配置

4、启动服务

5、Gitlab发送邮件测试

八、使用Gitlab

1、Gitlab创建项目组

2、Gitlab去掉用户的自动注册功能(安全起见)

3、Gitlab 开发代码提交处理流程

1、公司代码提交合并流程

2、创建管理员用户

3、将用户添加到刚才创建的群组中

4、 使用alan的账户登录gitlab并创建项目

5、添加SSH公钥到Gitlab中

6、使用gitlab

7、分支合并

九、Gitlab 备份与恢复(扩展)

1、查看系统版本和软件版本

2、数据备份

1、查看备份相关的配置项

2、执行备份命令进行备份

3、添加到 crontab 中定时执行

4、设置备份保留时长

3、数据恢复

1、安装部署 gitlab server

2、恢复 gitlab

4、恢复完成启动服务


一、Git、Github、Gitlab 的区别

Git是版本控制系统

Github是在线的基于Git的代码托管服务。 GitHub是2008年由Ruby on Rails编写而成。GitHub同时提供付费账户和免费账户。这两种账户都可以创建公开的代码仓库,可以创建私有的代码仓库

Gitlab 创建免费的私人repo

二:Git介绍

Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

1、Git 与 SVN 区别

GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等。

如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征。

Git 与 SVN 区别点:

GIT是分布式的,SVN不是:这是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别。

GIT把内容按元数据方式存储,而SVN是按文件:所有的资源控制系统都是把文件的元信息隐藏在一个类似.svn,.cvs等的文件夹里。

GIT分支和SVN的分支不同:分支在SVN中一点不特别,就是版本库中的另外的一个目录。

GIT没有一个全局的版本号,而SVN有:目前为止这是跟SVN相比GIT缺少的最大的一个特征。

GIT的内容完整性要优于SVN:GIT的内容存储使用的是SHA-1哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏。

2、Git工作流程

git 工作流程

克隆 Git 资源作为工作目录。

在克隆的资源上添加或修改文件。

如果其他人修改了,你可以更新资源。

在提交前查看修改。

提交修改。

在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

Git 的工作流程示意图:

git的工作区、暂存区和版本库

工作区:就是你在电脑里能看到的目录。

暂存区:英文叫stage, 或index。一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。

版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

工作区、版本库中的暂存区和版本库之间的关系的示意图:

图中左侧为工作区,右侧为版本库。在版本库中标记为 "index" 的区域是暂存区(stage, index),标记为 "master" 的是 master 分支所代表的目录树。

图中 "HEAD" 实际是指向 master 分支的一个"游标"。所以图示的命令中出现 HEAD 的地方可以用 master 来替换。

图中的 objects 标识的区域为 Git 的对象库,实际位于 ".git/objects" 目录下,里面包含了创建的各种对象及内容。

当对工作区修改(或新增)的文件执行 "git add" 命令时,暂存区的目录树被更新,同时工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中。

当执行提交操作(git commit)时,暂存区的目录树写到版本库(对象库)中,master 分支会做相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。

当执行 "git reset HEAD" 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响。

当执行 "git rm --cached <file>" 命令时,会直接从暂存区删除文件,工作区则不做出改变。

当执行 "git checkout ." 或者 "git checkout -- <file>" 命令时,会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区的改动。

当执行 "git checkout HEAD ." 或者 "git checkout HEAD <file>" 命令时,会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。

三、git安装

1、yum安装

环境:linux的centos操作系统

[root@localhost ~]#  yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel git-core
[root@localhost ~]#  git --version
git version 1.8.3.1

2.Windows 平台上安装

在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行:

安装包下载地址:Git for Windows

完成安装之后,就可以使用命令行的 git 工具(已经自带了 ssh 客户端)了,另外还有一个图形界面的 Git 项目管理工具。

在开始菜单里找到"Git"->"Git Bash",会弹出 Git 命令窗口,你可以在该窗口进行 Git 操作。

3、Mac 平台上安装

在 Mac 平台上安装 Git 最容易的当属使用图形化的 Git 安装工具,下载地址为:git-osx-installer download | SourceForge.net

安装界面如下所示:

四、git使用

Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量。

这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:

/etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。

~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。

当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。

在 Windows 系统上,Git 会找寻用户主目录下的 .gitconfig 文件。主目录即 $HOME 变量指定的目录,一般都是 C:\Documents and Settings$USER。

此外,Git 还会尝试找寻 /etc/gitconfig 文件,只不过看当初 Git 装在什么目录,就以此作为根目录来定位。

1、Git 用户信息

配置个人的用户名称和电子邮件地址:

[root@localhost ~]# yum -y install git
[root@localhost ~]# git config --global user.name "Chrismasman"
[root@localhost ~]# git config --global user.email "blg_uzi@163.com"

如果用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。

如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

2、查看配置信息

[root@localhost ~]# git config --list
user.name=Chrismasman
user.email=blg_uzi@163.com

这些配置我们也可以在 ~/.gitconfig 或 /etc/gitconfig 看到,如下所示:

[root@localhost ~]# cat ~/.gitconfig
[user]name = Chrismasmanemail = blg_uzi@163.com

3、Git 使用

1、ssh 链接

客户机上产生公钥上传到gitlab的SSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全

2、ssh连接github

登录github,这是github的主页(如果没有账户需要注册)。使用之前我们先注册一个github的账号,方便日后的使用。https://github.com

以ssh连接github为例:

使用之前先看一下git的常用命令

git init                    # 初始化
git add main.cpp            # 将某一个文件添加到暂存区
git add .                   # 将文件夹下的所有的文件添加到暂存区
git commit -m 'note'        # 将暂存区中的文件保存成为某一个版本
git log                     # 查看所有的版本日志
git status                  # 查看现在暂存区的状况
git diff                    # 查看现在文件与上一个提交-commit版本的区别
git reset --hard HEAD^      # 回到上一个版本
git reset --hard XXXXX      # XXX为版本编号,回到某一个版本
git reflog                  # 查看命令历史,以便确定要回到哪个版本
git pull origin master      # 从主分支pull到本地
git push -u origin master   # 从本地push到主分支
git pull                    # pull默认主分支
git push                    # push默认主分支 ...
git branch                  # 查看分支,*号在哪,就说明当前位于哪个分支
git branch --list           # 效果跟git branch一样
git branch xxx              # 创建一个分支
git checkout -b xxx         # 创建一个分支,并切换到该分支
git branch -d xxx           # 删除一个分支
git merge xxx               # 将xxx分支合并到当前的分支

(1)先在虚拟机创建公钥

[root@localhost ~]# ssh-keygen
[root@localhost ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6duPrTpcYbAGsewOwdViVh430pHBz0fKZhBCdcZ/F68GeZHzBYUZyvkqjWkpW/K306QRJFd0FqI3rAsH3XWC87Nacc/34yqD3uhNerG3ANY8NB6J/HI03AEJfaQgdpy70tcnN1ZcWMwTYdOWhn/LIb3s9VqLqPZI83FdU9qxEdwSPNQjurq1XnAXGNUSwJwBO2PeW6S9JuyUwfNKMyNOvzEu5anmMLWYwx2vChJu+qeu+XpndonhnBprtiKpFjFbFl/UB1kSBAokFMbwwakttzMDxZe2+PNELIQ5pk1THV5dvxBvHTUd117XYYwhV6eOOu9uGZ3a7cKJ1Bp2TVjXJ root@localhost.localdomain

(2)将公钥添加到github的SSH keys中

然后就可以从github克隆文件到本地服务器,或者从本地服务器上传文件到远程服务器

(1)从github克隆文件到本地服务器(首先github中需要提前存在文件/仓库)

先在github上创建仓库

我这里已经有一个仓库了,点击new创建新仓库

创建好了

要想克隆文件到本地服务器需要有这个仓库的ssh连接地址

使用git命令克隆文件到本地服务器

[root@localhost ~]# git clone git@github.com:Chrismasman/cloud2201.git
正克隆到 'cloud2201'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
接收对象中: 100% (3/3), done.
[root@localhost ~]# ls
anaconda-ks.cfg   cloud2201  

 (2)从本地服务器上传文件到远程github仓库

[root@localhost ~]# mkdir alice
[root@localhost ~]# cd alice/
[root@localhost alice]# git init   #初始化
初始化空的 Git 版本库于 /root/alice/.git/
[root@localhost alice]# ls -a
.  ..  .git
[root@localhost alice]# ls -a .git/
.  ..  branches  config  description  HEAD  hooks  info  objects  refs
[root@localhost alice]# echo "a" > a
[root@localhost alice]# ls
a
[root@localhost alice]# git add a  #添加文件a到git的暂存区
[root@localhost alice]# git commit -m "a-1"   #提交所有代码到本机的版本库,版本号为a-1
[master(根提交) 1493877] a-11 file changed, 1 insertion(+)create mode 100644 a
[root@localhost alice]# git remote add origin git@github.com:Chrismasman/cloud2201.git #添加远程项目地址(可从项目主页复制)
[root@localhost alice]# git branch  #查看分支
* master
[root@localhost alice]# git push -u origin master  #将本地的更改提交到远程服务器
Counting objects: 3, done.
Writing objects: 100% (3/3), 201 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/Chrismasman/cloud2201/pull/new/master
remote:
To git@github.com:Chrismasman/cloud2201.git* [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

在远程仓库github中查看是否多了一个分支

(3)分支管理

1、 你创建分支的时候,目录下有哪些文件(这个取决于分支),那么这个分支里就有哪些文件 

[root@localhost alice]# git branchalanAlice
* master
[root@localhost alice]# git checkout alice
切换到分支 'Alice'
[root@localhost alice]# ls
a
[root@localhost alice]# git branch zzz
[root@localhost alice]# git checkout zzz
切换到分支 'zzz'
[root@localhost alice]# ls
a          #因为我们是在Alice分支时创建的zzz分支,所以,刚创建的zzz分支中就有a

2、在哪个分支下修改就提交到哪个分支

[root@localhost alice]# git branchalanAlicemaster
* zzz
[root@localhost alice]# echo "aaaaaa">a
[root@localhost alice]# git add a
[root@localhost alice]# git commit -m "aa-1"
[zzz 5d6ce44] aa-11 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost alice]# git push -u origin master
分支 master 设置为跟踪来自 origin 的远程分支 master。
Everything up-to-date                    #因为不是在master分支下修改的,所以无法提交到master
[root@localhost alice]# git push -u origin zzz
Counting objects: 5, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'zzz' on GitHub by visiting:
remote:      https://github.com/Chrismasman/cloud2201/pull/new/zzz
remote:
To git@github.com:Chrismasman/cloud2201.git* [new branch]      zzz -> zzz
分支 zzz 设置为跟踪来自 origin 的远程分支 zzz。

3、将修改后的文件上传到github远程仓库会自动覆盖文件中原有的内容并且不会询问

[root@localhost alice]# cat a
aaaaaa
[root@localhost alice]# echo "bbbb">a
[root@localhost alice]# cat a
bbbb
[root@localhost alice]# git add a
[root@localhost alice]# git commit -m "aa-2"
[zzz 72b3951] aa-21 file changed, 1 insertion(+)
[root@localhost alice]# git push -u origin zzz
Counting objects: 5, done.
Writing objects: 100% (3/3), 237 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:Chrismasman/cloud2201.git5d6ce44..72b3951  zzz -> zzz
分支 zzz 设置为跟踪来自 origin 的远程分支 zzz。

再去看github的zzz分支中的内容

(4)版本回退

[root@localhost alice]# git branch
* alanalicemasterzzz
[root@localhost alice]# ls
a
[root@localhost alice]# echo "bbbb" >b
[root@localhost alice]# git add b
[root@localhost alice]# git commit -m "b-1"
[alan cdf6cff] b-11 file changed, 1 insertion(+)create mode 100644 b
[root@localhost alice]# echo "ccccc" > c
[root@localhost alice]# git add c
[root@localhost alice]# git commit -m "c-1"
[alan b9d59ed] c-11 file changed, 1 insertion(+)create mode 100644 c
[root@localhost alice]# ls        #现在我们有三个文件
a  b  c
[root@localhost alice]# git log        #查看版本号
commit b9d59ed45ce43b4a208aa1325d8f74867309784b
Author: Chrismasman <blg_uzi@163.com>
Date:   Wed May 11 23:50:05 2022 +0800c-1commit cdf6cff90b5727567181a1a49b127f7a184c1f26
Author: Chrismasman <blg_uzi@163.com>
Date:   Wed May 11 23:49:47 2022 +0800b-1commit 149387756d98286d4739aaec8ddd08c8ab8322e1
Author: Chrismasman <blg_uzi@163.com>
Date:   Wed May 11 22:30:43 2022 +0800a-1
[root@localhost alice]# git reset --hard 149387   #回退到版本号a-1
HEAD 现在位于 1493877 a-1
[root@localhost alice]# ls
a                                  #现在只有一个a文件了
[root@localhost alice]# git reflog     #用这个命令查看命令历史
1493877 HEAD@{0}: reset: moving to 149387
b9d59ed HEAD@{1}: commit: c-1
cdf6cff HEAD@{2}: commit: b-1
1493877 HEAD@{3}: reset: moving to 149387
49a8e48 HEAD@{4}: commit: a-2
5d71c49 HEAD@{5}: commit: a-1
1493877 HEAD@{6}: checkout: moving from zzz to alan
7bd0359 HEAD@{7}: commit: aa-2
72b3951 HEAD@{8}: commit: aa-2
5d6ce44 HEAD@{9}: reset: moving to 5d6ce44
8de0dd4 HEAD@{10}: commit: aa-1
1493877 HEAD@{11}: reset: moving to HEAD^
5d6ce44 HEAD@{12}: commit: aa-1
1493877 HEAD@{13}: checkout: moving from alice to zzz
1493877 HEAD@{14}: checkout: moving from master to alice
1493877 HEAD@{15}: commit (initial): a-1
[root@localhost alice]# git reset --hard b9d59ed    #在回退到c-1的版本
HEAD 现在位于 b9d59ed c-1
[root@localhost alice]# ls
a  b  c修改完后记得push到远程仓库(这里就省略了)

(5)注意

如果在github远程仓库创建了一个文件,但是没有同步到本地仓库,而现在在本地仓库修改后准备push到github远程仓库,是无法上传的,会报错,需要先同步一下

五、安装本地 Git 服务器

[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
[root@localhost ~]# mkdir /git-root/
[root@localhost ~]# cd /git-root/
[root@localhost git-root]# git init --bare zcg.git
[root@localhost git-root]# chown -R git.git zcg.git/   #给项目添加权限
[root@localhost git-root]# ssh-copy-id git@192.168.242.144  #推送公钥
[root@localhost git-root]# cd /opt/
[root@localhost opt]# git clone git@192.168.242.144:/git-root/zcg.git/ #拉取项目
.....
Are you sure you want to continue connecting (yes/no)? yes
....
[root@localhost opt]# ls
zcg
[root@localhost opt]# cd zcg
[root@localhost zcg]# ls -a
[root@localhost zcg]# ls -a
.  ..    .git
[root@localhost zcg]# ls -a .git/
.  ..  branches  COMMIT_EDITMSG  config  description  HEAD  hooks  index  info  logs  objects  refs
[root@localhost zcg]# echo "hello"> a
[root@localhost zcg]# cat a
hello
[root@localhost zcg]# git add a
[root@localhost zcg]# git commit -m "a"
...
[root@localhost zcg]# git push -u origin master
...* [new branch]      master -> master

六、Gitlab server部署

准备一台内存至少4G的虚拟机

1、安装Gitlab

1、安装Gitlab-ce

1、修改主机名(可做可不做)
2、关闭防火墙
3、关闭selinux
4、开启邮件服务systemctl start postfixsystemctl enable postfix
5、域名解析(可做可不做)
vim /etc/hosts
192.168.242.145 www.zcg666.com
6、安装Gitlab依赖包
[root@localhost ~]# yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python
7、添加啊Gitlab安装源(以下任选一个,不建议官方的安装源,官方下载的包比较大)
阿里源
[root@localhost ~]# vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
Repo_gpgcheck=0
Enabled=1
gpgcheck=0清华源:
[root@localhost ~]# vim gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1[root@localhost ~]# vim gitlab-ee.repo
[gitlab-ee]
name=Gitlab EE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/yum/el$releasever/
gpgcheck=0
enabled=1官方源:
[root@localhost ~]# vim runner_gitlab-ci-multi-runner.repo
[runner_gitlab-ci-multi-runner]
name=runner_gitlab-ci-multi-runner
baseurl=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300[runner_gitlab-ci-multi-runner-source]
name=runner_gitlab-ci-multi-runner-source
baseurl=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=3008、安装Gitlab(根据需要选择 ce/ee)
下载ce
[root@localhost ~]# yum -y install gitlab-ce   (包比较大,耐心等待)
...
安装成功...[root@localhost ~]# head -1 /opt/gitlab/version-manifest.txt #查看版本
gitlab-ce 14.10.2

2、Gitlab 配置登录链接

[root@localhost ~]# vim /etc/gitlab/gitlab.rb
***
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
# 没有域名,可以设置为本机IP地址
external_url 'http://192.168.242.145'
***

3、初始化 Gitlab (第一次使用配置时间较长)

[root@localhost ~]#  gitlab-ctl reconfigure
.....
......
gitlab Reconfigured!    #看到这个说明初始化完成

4、启动Gitlab服务

[root@localhost ~]# gitlab-ctl start
ok: run: alertmanager: (pid 3429) 48s
ok: run: gitaly: (pid 3444) 47s
ok: run: gitlab-exporter: (pid 3400) 51s
ok: run: gitlab-kas: (pid 3376) 53s
ok: run: gitlab-workhorse: (pid 3384) 52s
ok: run: grafana: (pid 3445) 47s
ok: run: logrotate: (pid 2529) 262s
ok: run: nginx: (pid 2979) 172s
ok: run: node-exporter: (pid 3396) 52s
ok: run: postgres-exporter: (pid 3439) 47s
ok: run: postgresql: (pid 2721) 239s
ok: run: prometheus: (pid 3408) 50s
ok: run: puma: (pid 2913) 190s
ok: run: redis: (pid 2559) 256s
ok: run: redis-exporter: (pid 3403) 50s
ok: run: sidekiq: (pid 2929) 184s

2、使用Gitlab

1、Gitlab 服务管理

gitlab-ctl start # 启动所有 gitlab 组件;
gitlab-ctl stop   # 停止所有 gitlab 组件;
gitlab-ctl restart # 重启所有 gitlab 组件;
gitlab-ctl status    # 查看服务状态;
gitlab-ctl reconfigure    # 初始化服务;
vim /etc/gitlab/gitlab.rb   # 修改默认的配置文件;
gitlab-ctl tail    # 查看日志;

2、登陆Gitlab

需要先指定root用户,并设置密码

[root@localhost ~]# gitlab-rails console -e production   #耐心等待
--------------------------------------------------------------------------------Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]GitLab:       14.10.2 (07d12f3fd11) FOSSGitLab Shell: 13.25.1PostgreSQL:   12.7
------------------------------------------------------------[ booted in 26.03s ]
Loading production environment (Rails 6.1.4.7)
irb(main):001:0> u=User.where(id:1).first   #定义为root用户
=> #<User id:1 @root>
irb(main):002:0> u.password='HanJing@123'  #设置root密码, 注意需要符合密码强度
=> "HanJing@123"
irb(main):003:0> u.password_confirmation='HanJing@123'  #确认当前密码
=> "HanJing@123"
irb(main):004:0> u.save!     #保存操作
=> true
irb(main):005:0> quit     #退出交互界面

浏览器访问192.168.242.145

六、Gitlab 设置 HTTPS 方式

1、创建私有密钥

需要做本地域名解析vim /etc/hosts
192.168.242.145 www.zcg666.com
windows上也要做域名解析
[root@localhost ~]# mkdir /etc/gitlab/ssl
[root@localhost ~]# openssl genrsa -out "/etc/gitlab/ssl/www.zcg666.com.key" 2048
Generating RSA private key, 2048 bit long modulus
...............+++
...............................................................................+++

2、创建私有证书

[root@localhost ~]# openssl req -new -key "/etc/gitlab/ssl/www.zcg666.com.key" -out "/etc/gitlab/ssl/www.zcg666.com.csr"
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:zcg
Locality Name (eg, city) [Default City]:zz
Organization Name (eg, company) [Default Company Ltd]:  #输入空格,然后回车
Organizational Unit Name (eg, section) []:  #输入空格,然后回车
Common Name (eg, your name or your server's hostname) []:www.zcg666.com
Email Address []:blg_uzi@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:  #直接回车

3、创建CRT签署证书

[root@localhost ~]# openssl x509 -req -days 365 -in "/etc/gitlab/ssl/www.zcg666.com.csr" -signkey "/etc/gitlab/ssl/www.zcg666.com.key" -out "/etc/gitlab/ssl/www.zcg666.com.crt"
[root@localhost ~]# openssl dhparam -out /etc/gitlab/ssl/dhparam.pem 2048
Signature ok
subject=/C=cn/ST=sh/L=sh/O= /OU= /CN=www.zcg666.com/emailAddress=blg_uzi@163.com
Getting Private key

4、创建pem证书

[root@localhost ~]# openssl dhparam -out /etc/gitlab/ssl/dhparam.pem 2048
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
........................................................+................................................................................+.....................................+..................................................................................+..............................................+..................................................................................................................................+..+........................................................................................................................................+..............................................................................................................................................................................+......+..............+.....................................................+.................+.......................................................................................+..+.................................................................................................................................................+..........................................................+.............+.........+...........................................................+........................................................................................................................................................................................................................................+...................................................................................................................................................................................................................................................................................................................++*++*

5、查看生成的证书

[root@localhost ~]# ls /etc/gitlab/ssl
dhparam.pem  www.zcg666.com.crt  www.zcg666.com.csr  www.zcg666.com.key

6、更改文件权限

[root@localhost ~]# chmod 600 /etc/gitlab/ssl/*

7、配置gitlab

[root@localhost ~]# cp /etc/gitlab/gitlab.rb{,.bak}
[root@localhost ~]# vim /etc/gitlab/gitlab.rb
## 更改如下
external_url 'https://www.zcg666.com'
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/www.zcg666.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/www.zcg666.com.key"

8、初始化gitlab相关服务配置

[root@localhost ~]# gitlab-ctl reconfigure    #耐心等待
...
gitlab Reconfigured!
# 出现这个表示配置没有问题!

9、nginx配置

[root@cicd-gitlab ~]# cp /var/opt/gitlab/nginx/conf/gitlab-http.conf{,.bak}
[root@cicd-gitlab ~]# vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
[如果可以正常访问,这里可以不做配置]37   server_name www.zcg666.com; #在此行下面添加38行的内容38   rewrite ^(.*)$ https://$host$1 permanent;

10、重启gitlab

[root@localhost ~]# gitlab-ctl restart
ok: run: alertmanager: (pid 6526) 1s
ok: run: gitaly: (pid 6543) 0s
ok: run: gitlab-monitor: (pid 6556) 0s
ok: run: gitlab-workhorse: (pid 6579) 1s
ok: run: logrotate: (pid 6589) 0s
ok: run: nginx: (pid 6597) 1s
ok: run: node-exporter: (pid 6681) 0s
ok: run: postgres-exporter: (pid 6687) 1s
ok: run: postgresql: (pid 6698) 0s
ok: run: prometheus: (pid 6706) 0s
ok: run: redis: (pid 6722) 0s
ok: run: redis-exporter: (pid 6856) 0s
ok: run: sidekiq: (pid 6866) 0s
ok: run: unicorn: (pid 6880) 0s
#  可以看出gitlab的所有服务重启完成

11、浏览器登录gitlab

七、Gitlab添加SMTP邮件功能

1、配置邮件功能

[root@localhost ~]# grep -P "^[^#].*smtp_|user_email|gitlab_email" /etc/gitlab/gitlab.rb
通过搜索功能搜索到以下内容,并进行修改
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "blg_uzi@163.com"
gitlab_rails['smtp_password'] = "EJLAUKIJOKTSYFUA"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'blg_uzi@163.com'
gitlab_rails['gitlab_email_display_name'] = 'Example'
gitlab_rails['gitlab_email_reply_to'] = '109...@qq.com'
gitlab_rails['gitlab_email_subject_suffix'] = 'gitlab'
# gitlab_rails['gitlab_email_smime_enabled'] = false
# gitlab_rails['gitlab_email_smime_key_file'] = '/etc/gitlab/ssl/gitlab_smime.key'
# gitlab_rails['gitlab_email_smime_cert_file'] = '/etc/gitlab/ssl/gitlab_smime.crt'
# gitlab_rails['gitlab_email_smime_ca_certs_file'] = '/etc/gitlab/ssl/gitlab_smime_cas.crt'

2、停止gitlab服务

[root@localhost ~]# gitlab-ctl stop
....

3、修改配置后初始化配置

[root@localhost ~]# gitlab-ctl reconfigure
......

4、启动服务

[root@localhost ~]# gitlab-ctl start

5、Gitlab发送邮件测试

[root@localhost ~]# gitlab-rails console   #启动时间有点长
--------------------------------------------------------------------------------Ruby:         ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]GitLab:       14.10.2 (07d12f3fd11) FOSSGitLab Shell: 13.25.1PostgreSQL:   12.7
------------------------------------------------------------[ booted in 59.77s ]
Loading production environment (Rails 6.1.4.7)
irb(main):001:0> Notify.test_email('109...@qq.com', 'Message Subject', 'Message Body').deliver_now

发送成功

八、使用Gitlab

1、Gitlab创建项目组

2、Gitlab去掉用户的自动注册功能(安全起见)

admin are -> settings -> Sign-up Restrictions 去掉钩钩,然后拉到最下面保存,重新登录

3、Gitlab 开发代码提交处理流程

1、公司代码提交合并流程

PM(项目主管/项目经理)在gitlab创建任务,分配给开发人员

开发人员领取任务后,在本地使用git clone拉取代码库

开发人员创建开发分支(git checkout -b dev),并进行开发

开发人员完成之后,提交到本地仓库(git commit )

开发人员在gitlab界面上申请分支合并请求(Merge request)

PM在gitlab上查看提交和代码修改情况,确认无误后,确认将开发人员的分支合并到主分支(master)

开发人员在gitlab上Mark done确认开发完成,并关闭issue。这一步在提交合并请求时可以通过描述中填写"close #1"等字样,可以直接关闭issue

2、创建管理员用户

利用刚才的步骤再创建一个普通用户

3、将用户添加到刚才创建的群组中

4、 使用alan的账户登录gitlab并创建项目

5、添加SSH公钥到Gitlab中

1、在虚拟机上创建一个名字与alan相同的用户,并创建公钥,将公钥添加到Gitlab的alan用户的SSH配置中

用另一台虚拟机作为客户端,使用gitlab
[root@localhost ~]# useradd alan
[root@localhost ~]# passwd alan  设置一个秘密
...
[root@localhost ~]# su - alan
[alan@localhost ~]$ ssh-keygen
...
[alan@localhost ~]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdly2hca3vaN5+s5tAtADgee/jJPaanRmXvyp1IAy1dCVkP9VRBkJM01zwx/sCpIx9puKR9SQi9Ak+nAyxIMQFdumHApoO8cTGNvUAvHrNR1lzkzMGNakMYEiC3LuSmn8wpsrahmbnAtlxVwNMT/3eWMF7H7C5gBDaBEQEzD18Olcq1ifhOuLbN4e/nCPOseXXqHTqmzlZbDspQMCrgx58oQKx7BzzzAoFYUjdCmina7wrHLgrR9+CqYgsGvnvyJKFfomPTLcM9ShP6j7GZbFvmWszjyXtfVrERfoZ9zJd47G+fzo9OeIbVYddjCFFVDx6gEWZ//mXnO5c7zCco+zR alan@localhost.localdomain

复制公钥内容,添加到gitlab(使用alan用户添加,好像不能使用其他用户)

然后就可以使用虚拟机从gitlab远程仓库克隆文件到本地,或者从虚拟机上传文件到gitlab仓库

6、使用gitlab

在虚拟机上为alan用户配置git
[alan@localhost ~]$ git config --global user.name "alan"
[alan@localhost ~]$ git config --global user.email "blg_uzi@163.com"
[alan@localhost ~]$ git clone git@192.168.242.145:123/best.git
正克隆到 'best'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
接收对象中: 100% (3/3), done.
[alan@localhost ~]$ ls
best
[alan@localhost ~]$ cd best/
[alan@localhost best]$ ls -a
.  ..  .git  README.md
[alan@localhost best]$ git push -u origin main
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 254 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 git@192.168.242.145:123/best.git! [remote rejected] main -> main (pre-receive hook declined)
error: 无法推送一些引用到 'git@192.168.242.145:123/best.git'
注意:会看到无法上传到main分支,因为gitlab的系统设置是默认主分支受保护,如果想往主分支上推送文件,需要先将Protecd branch功能取消,才能往主分支上推送文件。而我们自己创建的分支是可以往上面推送文件的
[alan@localhost best]$ git checkout -b a
切换到一个新分支 'a'
[alan@localhost best]$ echo "aaaa" > a
[alan@localhost best]$ git add a
[alan@localhost best]$ git commit -m "a"
[a 8aeb0da] a1 file changed, 1 insertion(+), 1 deletion(-)
[alan@localhost best]$ git push -u origin a
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 476 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for a, visit:
remote:   https://192.168.242.145/123/best/-/merge_requests/new?merge_request%5Bsource_branch%5D=a
remote:
To git@192.168.242.145:123/best.git* [new branch]      a -> a
分支 a 设置为跟踪来自 origin 的远程分支 a。
#我们又创建了一个分支,修改内容后push到了gitlab仓库

取消保护分支功能的方法

出现这句话就可以推送引用到main分支了

[alan@localhost best]$ ls
README.md
[alan@localhost best]$ touch aaaa
[alan@localhost best]$ git add .
[alan@localhost best]$ git commit -m "m"
[main 06e0fd5] m1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 aaaa
[alan@localhost best]$ git branch
* main
[alan@localhost best]$ git push -u origin main
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.242.145:123/best.gitcefb454..06e0fd5  main -> main
分支 main 设置为跟踪来自 origin 的远程分支 main。
这就可以推送引用到main分支了

7、分支合并

再创建一个分支
[alan@localhost best]$ git checkout -b b
切换到一个新分支 'b'
[alan@localhost best]$ echo "bbbb" > b
[alan@localhost best]$ git add b
[alan@localhost best]$ git commit -m "b"
[b 5212d90] b1 file changed, 1 insertion(+)create mode 100644 b
[alan@localhost best]$ git push -u origin b
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for b, visit:
remote:   https://192.168.242.145/123/best/-/merge_requests/new?merge_request%5Bsource_branch%5D=b
remote:
To git@192.168.242.145:123/best.git* [new branch]      b -> b
分支 b 设置为跟踪来自 origin 的远程分支 b。

在Gitlab上进行分支合并

admin用户

这样就合并完成了

九、Gitlab 备份与恢复(扩展)

1、查看系统版本和软件版本

[root@xingdian-git ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

13.6.1

[root@xingdian-git ~]# cat /etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

2、数据备份

1、查看备份相关的配置项

[root@qfedu.com ~]# vim /etc/gitlab/gitlab.rb

gitlab_rails['manage_backup_path'] = true

gitlab_rails['backup_path'] = "/data/gitlab/backups"

该项定义了默认备份出文件的路径,可以通过修改该配置,并执行 gitlab-ctl reconfigure 或者 gitlab-ctl  restart 重启服务生效。

2、执行备份命令进行备份

[root@qfedu.com ~]# /opt/gitlab/bin/gitlab-rake gitlab:backup:create

3、添加到 crontab 中定时执行

[root@qfedu.com ~]# crontab -e

0 2 * * * bash /opt/gitlab/bin/gitlab-rake gitlab:backup:create

可以到/data/gitlab/backups找到备份包,解压查看,会发现备份的还是比较全面的,数据库、repositories、build、upload等分类还是比较清晰的。

4、设置备份保留时长

防止每天执行备份,有目录被爆满的风险,打开/etc/gitlab/gitlab.rb配置文件,找到如下配置:

[root@qfedu.com ~]# vim /etc/gitlab/gitlab.rb

gitlab_rails['backup_keep_time'] = 604800

设置备份保留7天(7360024=604800),秒为单位,如果想增大或减小,可以直接在该处配置,并通过gitlab-ctl restart 重启服务生效。

备份完成,会在备份目录中生成一个当天日期的tar包。

3、数据恢复

1、安装部署 gitlab server

具体步骤参见上面:gitlab server 搭建过程

2、恢复 gitlab

1、查看备份相关的配置项

[root@qfedu.com ~]# vim /etc/gitlab/gitlab.rb

gitlab_rails['backup_path'] = "/data/gitlab/backups"

修改该配置,定义了默认备份出文件的路径,并执行 gitlab-ctl reconfigure 或者 gitlab-ctl  restart 重启服务生效。

2、恢复前需要先停掉数据连接服务

[root@qfedu.com ~]# gitlab-ctl stop unicorn

[root@qfedu.com ~]# gitlab-ctl stop sidekiq

如果是台新搭建的主机,不需要操作,理论上不停这两个服务也可以。停这两个服务是为了保证数据一致性。

如果是台新搭建的主机,不需要操作,理论上不停这两个服务也可以。停这两个服务是为了保证数据一致性。

3、同步备份文件到新服务器

将老服务器/data/gitlab/backups目录下的备份文件拷贝到新服务器上的/data/gitlab/backups

[root@qfedu.com gitlab]# rsync -avz 1530773117_2019_03_05_gitlab_backup.tar 10.0.1.86:/data/gitlab/backups/

注意权限:600权限是无权恢复的。 实验环境可改成了777,生产环境建议修改属主属组

[root@qfedu.com backups]# pwd

/data/gitlab/backups

[root@qfedu.com backups]# chown -R git.git 1530773117_2019_03_05_gitlab_backup.tar

[root@qfedu.com backups]# ll

total 17328900

-rwxrwxrwx 1 git git 17744793600 Jul  5 14:47 1530773117_2018_07_05_gitlab_backup.tar

4、执行命令进行恢复

后面再输入两次 yes 就完成恢复了。

[root@qfedu.com ~]# gitlab-rake gitlab:backup:restore BACKUP=1530773117_2018_07_05_gitlab_backup.tar

注意:backups 目录下保留一个备份文件可直接执行

4、恢复完成启动服务

恢复完成后,启动刚刚的两个服务,或者重启所有服务,再打开浏览器进行访问,发现数据和之前的一致:

[root@qfedu.com ~]# gitlab-ctl start unicorn

[root@qfedu.com ~]# gitlab-ctl start sidekiq

[root@qfedu.com ~]# gitlab-ctl restart

Gti版本控制系统Github与Gitlab的使用相关推荐

  1. 各家版本控制系统(VCS)对比:VSS、SVN、Git。代码托管平台对比:GitLab、GitHub、码云(Gitee)、SourceForge、DevCloud(华为软件开发云)

    参考文章1:几种常用的版本控制系统优缺点比较 参考文章2:Git,GitLab,GitHub,SVN ,CVS,VSS,码云的安装,用法,区别,进阶

  2. 版本控制系统(SVN,Git)与项目托管平台(Github,Gitee,Coding)

    1.版本控制系统 版本控制系统(version control system)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统不仅可以应用于软件源代码的文本文件,而且 ...

  3. 基于git(分布式版本控制系统)的各种服务器权限工具对比 Gitlab服务器搭建 以及邮箱、LDAP配置 实现公司多人协同开发

    前言 作为大一的小朋友,虽然学的专业和CS有那么一点关系,但放到实习的工作当中,那是完全不够用了:( 在实习老师的指导和帮助下,在实习的头个礼拜,逐步接触了解了git--开源分布式版本控制系统.并在第 ...

  4. 使用docker搭建gitlab版本控制系统

    1. GitLab 简介 GitLab 是一款基于 git 的开源代码仓库系统  GitLab 与著名的 GitHub 最大的区别就是:  允许我们搭建自己的 git 代码私有仓库,非常方便 2.安装 ...

  5. 【Git、GitHub、GitLab】四 Git文件重命名的简单方法以及使用git log查看版本演变历史

    上一篇文章学会了使用GIT四次提交建立一个有模有样的仓库.点击链接查看:[Git.GitHub.GitLab]三 Git基本命令之创建仓库并向仓库中添加文件. 本片文章记录git的文件重命名的简单方法 ...

  6. 分布式版本控制系统Git与项目托管平台Github相关概念、操作方法、开发流程与常用命令

    tip:有问题或者需要大厂内推的+我脉脉哦:丛培森 ٩( 'ω' )و [本文源址:http://blog.csdn.net/q1056843325/article/details/54667602 ...

  7. 分布式版本控制系统Git的下载、安装与使用其复制GitHub项目代码的方法

      本文介绍分布式开源版本控制系统Git的下载.安装,并基于Git实现克隆GitHub中项目代码的方法.   Git是一款开源软件,因此我们直接在Git的官方下载地址(https://git-scm. ...

  8. Git,Github和Gitlab简介和使用方法

    什么是Git Git是一个版本控制系统(Version Control System,VCS). 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 多年前,我在法国做第一 ...

  9. Git,Github和Gitlab简介和基本使用

    什么是Git Git是一个版本控制系统(Version Control System,VCS). 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 多年前,我在法国做第一 ...

最新文章

  1. php 字符串包含另一个字符串_leetcode1433_go_检查一个字符串是否可以打破另一个字符串...
  2. 一步步编写操作系统 45 用c语言编写内核2
  3. H264基本概念之 宏块、片和片组
  4. 老男孩36期运维脱产班---- 决心书
  5. 使用 XHGui 对 PHP 应用进行性能诊断
  6. 向域管理转型所遇到的问题
  7. Soft NMS算法笔记
  8. VS2015解决非Unicode编码包含中文字段无法编译的问题
  9. 【Python小程序】第2讲:如何将TXT文件转换成CSV文件?
  10. 项目规划管理 - 5
  11. 前台离岗提示语_安全温馨提示语大全.doc
  12. Python Day4 循环关键字和练习【零基础】
  13. 上海航芯|推出基于ACX200T的V2X解决方案
  14. html如何让文字图片同高,css里图片和文字如何等高
  15. Linux服务器在线测速
  16. 除了百度、搜狗搜索你还知道哪些好用的搜索引擎吗?
  17. 虫师 Selenium3 基于Python 生成HTML测试报告误区记录
  18. 性能测试跑分软件,专业测试软件 性能跑分有多强
  19. 2020 PHP高级面试题之设计模式(四)
  20. python 聚类分析实战案例:K-means算法(原理源码)

热门文章

  1. 微信思维导图(搜索)
  2. JavaScript:实现将字节数组转换为 base64 编码算法(附完整源码)
  3. 【期权】期权投资策略与盈亏计算
  4. 根域服务器物理服务器,为何根域名服务器只有13个?
  5. java fx插入图片,javafx在tableview中添加图像
  6. java sinh_Java Math.sinh() 方法
  7. 微信小程序NFC读写工具
  8. 计算机应用研究 相对好发一些,传感技术学报期刊好投吗_机械科学与技术录用率高吗_计算机工程与应用被拒还能再投吗...
  9. EVT、DVT、PVT、MP是什么意思
  10. 百万补贴!武汉市技术交易补贴申报条件、流程、材料