Git与Gitlab

一、Git

1.git基本命令

安装

yum install -y git
[root@gitlab ~]# yum install -y git
......
Updated:
  git.x86_64 0:1.8.3.1-20.el7                                                                           

Dependency Updated:
  perl-Git.noarch 0:1.8.3.1-20.el7                                                                      

Complete!

设置git用户对应邮箱

git config --globbal user.email "邮箱地址"
[root@gitlab ~/demo]# git config --global user.email "5******7@qq.com"

配置写入 ~/.gitconfig文件中

设置git用户

git config --global user.name "用户名"
[root@gitlab ~/demo]# git config --global user.name "QQ"
[root@gitlab ~/demo]# cat ~/.gitconfig 
[user]
    email = 5******7@qq.com
    name = QQ

配置写入 ~/.gitconfig文件中

初始化git目录

git init
[root@gitlab ~/demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@gitlab ~/demo]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs
[root@gitlab ~/demo]# cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true

查看暂存区状态

git status
[root@gitlab ~/demo]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@gitlab ~/demo]# touch file_01
[root@gitlab ~/demo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   file_01
nothing added to commit but untracked files present (use "git add" to track)

将文件上传至暂存区

git add 文件名 #将目录下指定文件添加到git暂存区中。
git add . #将目录中所有文件都添加到git暂存区中
[root@gitlab ~/demo]# git add file_01
[root@gitlab ~/demo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   file_01
#
[root@gitlab ~/demo]# touch file_02 file_03
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   file_01
#   new file:   file_02
#   new file:   file_03
#

将暂存区文件同步至本地仓库
注意:将暂存区文件同步至本地仓库时,需提前设置用户名及邮箱

git commit -m "描述" #将暂存区的文件同步至本地仓库。
[root@gitlab ~/demo]# git commit -m "Master_Branch touch file 01_02_03"
[master (root-commit) ae53165] Master_Branch touch file 01_02_03
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file_01
 create mode 100644 file_02
 create mode 100644 file_03
[root@gitlab ~/demo]# git status
# On branch master
nothing to commit, working directory clean
git commit -a -m "描述 #直接将本地目录下的文件直接同步至本地仓库
不推荐使用上述命令,原则上应将本地目录的文件添加至暂存区再同步至本地仓库

查看本地git目录下的git日志

git log
--oneline #查看本地git目录下的简要日志
--graph #查看本地git目录下的分支图
git log --graph --decorate --one-line
[root@gitlab ~/demo]# git log
commit ae53165afc8bc9569eff4b8d1c8ba7b374c133c0
Author: QQ <5******7@qq.com>
Date:   Fri Nov 1 09:25:09 2019 +0800

    Master_Branch touch file 01_02_03
[root@gitlab ~/demo]# git log --oneline
ae53165 Master_Branch touch file 01_02_03
[root@gitlab ~/demo]# git log --oneline --graph --color 
* ae53165 Master_Branch touch file 01_02_03

commit ID在整个git系统中是唯一,不重复的

git reflog #查看本地git目录下的全部日志
[root@gitlab ~/demo]# git reflog
ae53165 HEAD@{0}: commit (initial): Master_Branch touch file 01_02_03

对比本地目录文件与暂存区文件内容

git diff 文件
--cached #对比暂存区文件与本地仓库文件内容
git diff commitID #对比本地目录文件与本地仓库文件内容
[root@gitlab ~/demo]# echo test_diff_cached > file_01 
[root@gitlab ~/demo]# echo test_diff_local > file_02
[root@gitlab ~/demo]# echo test_diff >file_03
[root@gitlab ~/demo]# git add file_01
[root@gitlab ~/demo]# git commit -m "Master_Branch file01 new contents 'test_diff_cached'"
[master f193d42] Master_Branch file01 new contents 'test_diff_cached'
 1 file changed, 1 insertion(+)
[root@gitlab ~/demo]# git add file_02
#对比本地目录与缓存区文件差异--------------------------------------------------------------------
[root@gitlab ~/demo]# git diff file_01
[root@gitlab ~/demo]# git diff file_02
[root@gitlab ~/demo]# git diff file_03
diff --git a/file_03 b/file_03
index e69de29..c151392 100644
--- a/file_03 #a 表示变动前的版本
+++ b/file_03 #b 表示变动后的版本
@@ -0,0 +1 @@
+test_diff
#对比本地仓库与缓存区文件差异--------------------------------------------------------------------
[root@gitlab ~/demo]# git diff --cached file_01
[root@gitlab ~/demo]# git diff --cached file_02
diff --git a/file_02 b/file_02
index e69de29..0bc60da 100644
--- a/file_02
+++ b/file_02
@@ -0,0 +1 @@
+test_diff_local
[root@gitlab ~/demo]# git diff --cached file_03
#对比本地仓库与本地目录文件差异-----------------------------------------------------------------
diff --git a/file_02 b/file_02
index e69de29..0bc60da 100644
--- a/file_02
+++ b/file_02
@@ -0,0 +1 @@
+test_diff_local
diff --git a/file_03 b/file_03
index e69de29..c151392 100644
--- a/file_03
+++ b/file_03
@@ -0,0 +1 @@
+test_diff

将暂存区文件回退到本地目录(放弃本地目录下的文件修改)

git checkout -- 文件
[root@gitlab ~/demo]# touch file_05
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# echo "backspace from cache to local dir" >file_05 
[root@gitlab ~/demo]# cat file_05
backspace from cache to local dir
[root@gitlab ~/demo]# git checkout -- file_05
[root@gitlab ~/demo]# cat file_05

将本地仓库文件回退到暂存区(放弃暂存区修下的文件修改)

git reset HEAD 文件
[root@gitlab ~/demo]# echo "backspace from cache to local dir" >file_05 
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git commit -m "Master branch new file_05 include 'backspace from cache to local dir'"
[master dfbb5b1] Master branch new file_05 include 'backspace from cache to local dir'
 1 file changed, 1 insertion(+)
 create mode 100644 file_05
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git diff --cached file_05
diff --git a/file_05 b/file_05
index cf36643..e69de29 100644
--- a/file_05
+++ b/file_05
@@ -1 +0,0 @@
-backspace from cache to local dir
[root@gitlab ~/demo]# git reset HEAD file_05
Unstaged changes after reset:
M   file_05
[root@gitlab ~/demo]# git diff --cached file_05

将本地仓库的指针重定向到指定ID

git reset --hard commitID

常用于多次提交后,恢复文件内容使用;

不建议实际环境中使用该指令
[root@gitlab ~/demo]# cat file_05
backspace from cache to local dir
[root@gitlab ~/demo]# echo "new contents 1" >file_05
[root@gitlab ~/demo]# git commit -a -m "file_05 new contents 1"
[master b573b09] file_05 new contents 1
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab ~/demo]# echo "new contents 2" >file_05
[root@gitlab ~/demo]# git commit -a -m "file_05 new contents 2"
[master a65273a] file_05 new contents 2
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab ~/demo]# echo "new contents 3" >file_05
[root@gitlab ~/demo]# git commit -a -m "file_05 new contents 3"
[master b0e1463] file_05 new contents 3
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab ~/demo]# cat file_05
new contents 3
[root@gitlab ~/demo]# git log --oneline
b0e1463 file_05 new contents 3
a65273a file_05 new contents 2
b573b09 file_05 new contents 1
dfbb5b1 Master branch new file_05 include 'backspace from cache to local dir'
dc52d62 Master Branch file_02 file_03 new contents 'test diff' (supplement)
7c915e8 Master Branch file_02 file_03 new contents 'test diff'
f193d42 Master_Branch file01 new contents 'test_diff_cached'
ae53165 Master_Branch touch file 01_02_03
[root@gitlab ~/demo]# git reset --hard dfbb5b1
HEAD is now at dfbb5b1 Master branch new file_05 include 'backspace from cache to local dir'
[root@gitlab ~/demo]# cat file_05
backspace from cache to local dir
[root@gitlab ~/demo]# git log --oneline
dfbb5b1 Master branch new file_05 include 'backspace from cache to local dir'
dc52d62 Master Branch file_02 file_03 new contents 'test diff' (supplement)
7c915e8 Master Branch file_02 file_03 new contents 'test diff'
f193d42 Master_Branch file01 new contents 'test_diff_cached'
ae53165 Master_Branch touch file 01_02_03
[root@gitlab ~/demo]# git reflog 
dfbb5b1 HEAD@{0}: reset: moving to dfbb5b1
b0e1463 HEAD@{1}: commit: file_05 new contents 3
a65273a HEAD@{2}: commit: file_05 new contents 2
b573b09 HEAD@{3}: commit: file_05 new contents 1
dfbb5b1 HEAD@{4}: commit: Master branch new file_05 include 'backspace from cache to local dir'
dc52d62 HEAD@{5}: commit: Master Branch file_02 file_03 new contents 'test diff' (supplement)
7c915e8 HEAD@{6}: commit: Master Branch file_02 file_03 new contents 'test diff'
f193d42 HEAD@{7}: commit: Master_Branch file01 new contents 'test_diff_cached'
ae53165 HEAD@{8}: commit (initial): Master_Branch touch file 01_02_03

重命名文件

git mv 文件名1 文件名2
[root@gitlab ~/demo]# git mv file_05 file_04
[root@gitlab ~/demo]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    file_05 -> file_04
#
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04
git commit -m "Master Branch file_05 to file_04"
[master ee9b24d] Master Branch file_05 to file_04
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename file_05 => file_04 (100%)  

2.git分支管理

查看分支

git branch
[root@gitlab ~/demo]# git branch
* master

创建分支

git branch 分支名
[root@gitlab ~/demo]# git branch master
[root@gitlab ~/demo]# git branch
  dev
* master

切换分支

git checkout 分支名
[root@gitlab ~/demo]# git checkout dev
Switched to branch 'dev'
[root@gitlab ~/demo]# git branch
* dev
  master

合并分支

git merge 分支名

附:在实际使用中,通常将父分支合并到子分支,测试无误后,再将子分支合并到父分支上

[root@gitlab ~/demo]# git branch 
* dev
  master
[root@gitlab ~/demo]# touch file_06
[root@gitlab ~/demo]# echo Merge_Test_Dev >file_07
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git commit -m "Dev Branch new file_06 and file_07 Merge_Test_Dev"
[dev 5f1ac39] Dev Branch new file_06 and file_07 Merge_Test_Dev
 2 files changed, 1 insertion(+)
 create mode 100644 file_06
 create mode 100644 file_07
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_06  file_07
[root@gitlab ~/demo]# echo someting to test  > file_05 
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git commit -m "Master Brance new file_05 with someting to test"
[master a5622f5] Master Brance new file_05 with someting to test
 1 file changed, 1 insertion(+)
 create mode 100644 file_05
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_05
[root@gitlab ~/demo]# git checkout dev 
Switched to branch 'dev'
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_06  file_07
#合并分支---------------------------------------------------------------------------------------------
[root@gitlab ~/demo]# git merge master 
Merge branch 'master' into dev

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
".git/MERGE_MSG" 7L, 258C written
Merge made by the 'recursive' strategy.
 file_05 | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file_05
[root@gitlab ~/demo]# git branch 
* dev
  master
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_05  file_06  file_07
[root@gitlab ~/demo]# cat file_05
someting to test
[root@gitlab ~/demo]# git checkout master 
Switched to branch 'master'
[root@gitlab ~/demo]# git merge dev 
Updating a5622f5..a1128ab
Fast-forward
 file_06 | 0
 file_07 | 1 +
 2 files changed, 1 insertion(+)
 create mode 100644 file_06
 create mode 100644 file_07
[root@gitlab ~/demo]# git branch 
  dev
* master
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_05  file_06  file_07
[root@gitlab ~/demo]# cat file_07
Merge_Test_Dev
合并冲突
分支在合并过程中,同一个文件的同一行出现不同内容会导致合并冲突;发生冲突时需人工协商解决,无法自动解决
[root@gitlab ~/demo]# git checkout dev 
Switched to branch 'dev'
[root@gitlab ~/demo]# vim file_08
AAAA
BBBB

YYYY
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git commit -m "Dev Branch Merge confilct file_08"
[dev b1dbf6a] Dev Branch Merge confilct file_08
 1 file changed, 4 insertions(+)
 create mode 100644 file_08

[root@gitlab ~/demo]# git branch 
  dev
* master
[root@gitlab ~/demo]# vim file_08
AAAA
BBBB

XXXX
[root@gitlab ~/demo]# git checkout dev 
Switched to branch 'dev'
[root@gitlab ~/demo]# git branch 
* dev
  master
[root@gitlab ~/demo]# git merge master 
Auto-merging file_08
CONFLICT (add/add): Merge conflict in file_08
Automatic merge failed; fix conflicts and then commit the result.

解决方案

[root@gitlab ~/demo]# cat file_08
AAAA
BBBB

<<<<<<< HEAD
YYYY
=======
XXXX
>>>>>> master
[root@gitlab ~/demo]# cat file_08
AAAA
BBBB

YYYY
[root@gitlab ~/demo]# git add .
[root@gitlab ~/demo]# git commit -m "fix merge conflict from master to dev"
[dev 846e129] fix merge conflict from master to dev
[root@gitlab ~/demo]# git branch 
* dev
  master
[root@gitlab ~/demo]# git merge master
Already up-to-date.
[root@gitlab ~/demo]# git checkout master 
Switched to branch 'master'
[root@gitlab ~/demo]# git merge dev
Updating 26f0e2f..846e129
Fast-forward
 file_08 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab ~/demo]# cat file_08
AAAA
BBBB

YYYY

3.git标签管理

定义
当Git仓库内的数据发生变更时,为变更制作一个标签,这样通过标签就可以将版本库中的某次commit记录下来,便于我们后续将特定时期的数据取出。
简单来说:标签是版本库的一个快照。
作用
标签类似于commit-ID的一个别名,方便记忆

tag存在被篡改的风险,如同一个tag名称先后被绑定两次。标准操作建议使用commit-ID。
创建标签
git tag -a "标签名称" -m "描述" #为当前commit-ID创建标签
git tag -a "标签名称" commit-ID -m "描述" #为指定commit-ID创建标签
[root@gitlab ~/demo]# git tag -a "version_1.2" -m "total 8 file and fix merge conflict"
[root@gitlab ~/demo]# git tag -a "version_1.0" a1128ab -m "release version"

查看标签

git tag -l
[root@gitlab ~/demo]# git tag -l
version_1.0
version_1.2

查看标签对应的commit-ID

git show 标签名称
[root@gitlab ~/demo]# git show version_1.0
tag version_1.0
Tagger: QQ <5******7@qq.com>
Date:   Fri Nov 1 14:00:45 2019 +0800

release version

commit a1128abef476a5132a6bd253e881da58a4217245
Merge: 5f1ac39 a5622f5
Author: QQ <5******7@qq.com>
Date:   Fri Nov 1 11:42:38 2019 +0800

    Merge branch 'master' into dev
[root@gitlab ~/demo]# git show version_1.2
tag version_1.2
Tagger: QQ <5******7@qq.com>
Date:   Fri Nov 1 13:58:12 2019 +0800

total 8 file and fix merge conflict

commit 846e12912b9a56e13bca484520cde89448fe4519
Merge: b1dbf6a 26f0e2f
Author: QQ <5******7@qq.com>
Date:   Fri Nov 1 13:49:48 2019 +0800

    fix merge conflict from master to dev

删除标签

git tag -d "标签名称" #删除本地标签
git push origin --delete tag 标签名称 #删除一个远程标签
git push origin :ref/tags/标签名称 #删除一个远程标签
[root@gitlab ~/demo]# git tag -l
version_1.0
version_1.2
[root@gitlab ~/demo]# git tag -d version_1.2
Deleted tag 'version_1.2' (was 640f051)
[root@gitlab ~/demo]# git tag -l
version_1.0

推送标签

git push origin 标签名称 #推送一个本地标签到远程仓库
git push origin --tags #推送全部本地标签到远程仓库

4.连接远程仓库(基于Gitee)

添加远程仓库
添加远程仓库前,请先将本地账户的公钥与远程仓库账户绑定,进行免密交互

git remote add 仓库名 仓库地址
[root@gitlab ~/demo]# git remote add origin git@gitee.com:aspen_han/Demo.git
[root@gitlab ~/demo]# git remote -v
origin  git@gitee.com:aspen_han/Demo.git (fetch)
origin  git@gitee.com:aspen_han/Demo.git (push)

将本地仓库推向远程仓库

git push 仓库名 分支名
-u #指定默认仓库
-f #强制提交
[root@gitlab ~/demo]# git push -u origin master
The authenticity of host 'gitee.com (212.64.62.174)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.62.174' (ECDSA) to the list of known hosts.
Counting objects: 41, done.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (41/41), 3.43 KiB | 0 bytes/s, done.
Total 41 (delta 12), reused 0 (delta 0)
remote: Powered By Gitee.com
To git@gitee.com:a***n_han/Demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

将远程仓库下拉到本地

git clone 远程仓库地址 (首次使用)
git pull 仓库名 分支名
 [aspen_han@gitlab ~/demo]$ ls
[aspen_han@gitlab ~/demo]$ git clone git@gitee.com:aspen_han/Demo.git
Cloning into 'Demo'...
The authenticity of host 'gitee.com (212.64.62.174)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.62.174' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 41, done.
remote: Counting objects: 100% (41/41), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 41 (delta 12), reused 0 (delta 0)
Receiving objects: 100% (41/41), done.
Resolving deltas: 100% (12/12), done.
[aspen_han@gitlab ~/demo]$ ls
Demo
[root@gitlab ~/demo]# git pull origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitee.com:aspen_han/Demo
 * branch            master     -> FETCH_HEAD
Updating 846e129..d34930f
Fast-forward
 README.md | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 README.md
[root@gitlab ~/demo]# ls
file_01  file_02  file_03  file_04  file_05  file_06  file_07  file_08  README.md

查看远程仓库地址

git remote -v
[root@gitlab ~/demo]# git remote -v
origin  git@gitee.com:aspen_han/Demo.git (fetch)
origin  git@gitee.com:aspen_han/Demo.git (push)

二、Gitlab

1.概述

gitab是一个开源的远程仓库,分为社区版(ce)与企业版(ee)

2.优势

  • 离线同步
  • 分支管理

3.服务组成

  • nginx:作为gitlab的proxy代理,处理http/https以及静态资源访问请求
  • gitlab-workhorse:用于处理文件的上传和下载
  • gitlab-shell:用于处理git clone、git pull以及git push命令
  • Logrotate:用于处理日志的切割和打包等操作
  • Postgresql:用于保存所有的gitlab数据相关信息
  • Redis:用于缓存数据库的信息,加快前台访问速度以及读写交互

[root@gitlab /tmp]# gitlab-ctl status
run: alertmanager: (pid 12592) 34s; run: log: (pid 12478) 51s
run: gitaly: (pid 12537) 36s; run: log: (pid 11982) 124s
run: gitlab-monitor: (pid 12577) 35s; run: log: (pid 12384) 61s
run: gitlab-workhorse: (pid 12558) 36s; run: log: (pid 12306) 80s
run: logrotate: (pid 12324) 74s; run: log: (pid 12334) 73s
run: nginx: (pid 12299) 80s; run: log: (pid 12317) 77s
run: node-exporter: (pid 12570) 35s; run: log: (pid 12365) 65s
run: postgres-exporter: (pid 12602) 33s; run: log: (pid 12520) 45s
run: postgresql: (pid 12093) 119s; run: log: (pid 12104) 118s
run: redis: (pid 11943) 126s; run: log: (pid 11972) 125s
run: redis-exporter: (pid 12580) 34s; run: log: (pid 12454) 55s
run: sidekiq: (pid 12271) 87s; run: log: (pid 12281) 86s
run: unicorn: (pid 12243) 93s; run: log: (pid 12267) 92s

4.gitlab安装与配置

step0 安装软件依赖包(关闭防火墙与SELinux)

yum install -y curl openssh-server postfix wget
[root@gitlab ~/Demo]# yum install -y curl openssh-server postfix wget
......
Updated:
  curl.x86_64 0:7.29.0-54.el7                    openssh-server.x86_64 0:7.4p1-21.el7                   
Complete!

step1 本地安装gitlab
gitlab下载地址(清华大学源)

yum localinstall -y 安装包名称
[root@gitlab /tmp]# yum localinstall -y gitlab-ce-12.0.3-ce.0.el7.x86_64.rpm 
......
       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.

     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
......
Installed:
  gitlab-ce.x86_64 0:12.0.3-ce.0.el7                                                   

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7       checkpolicy.x86_64 0:2.5-8.el7          
  libcgroup.x86_64 0:0.41-21.el7               libsemanage-python.x86_64 0:2.5-14.el7  
  policycoreutils-python.x86_64 0:2.5-33.el7   python-IPy.noarch 0:0.75-6.el7          
  setools-libs.x86_64 0:3.3.8-4.el7           

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7                   audit-libs.x86_64 0:2.8.5-4.el7         
  policycoreutils.x86_64 0:2.5-33.el7         

Complete!

step2 配置gitlab服务、访问域名以及邮箱

vim /etc/gitlab/gitlab.rb

external_url '域名' #指定访问域名
gitlab_rails['gitlab_email_enabled'] = 'true' #开启邮件服务
gitlab_rails['gitlab_email_from'] = '发件人邮箱' #指定发件人邮箱
gitlab_rails['gitlab_display_name'] = '发件人' #指定发件人名称
gitlab_rails['smtp_enable'] = 'true #开启smtp服务
gitlab_rails['smtp_address'] = 'smtp服务器地址' #指定发件人邮箱smtp服务器地址
gitlab_rails['smtp_port'] = 'smtp端口' #指定发件人邮箱smtp服务器提供的端口(465)

gitlab_rails['smtp_user_name'] = '用户名' #指定发件人邮箱登录用户名
gitlab_rails['smtp_password'] = '授权码' #指定发件人邮箱登录授权码
gitlab_rails['smtp_domain'] = '域名' #指定发件人邮箱smtp服务器域名
gitlab_rails['smtp_authentication'] = 'login' #指定发件人邮箱登录方式
gitlab_rails['smtp_enable_starttls_auto'] = 'true' #启用Tls安全邮件服务
gitlab_rails['smtp_tls'] = 'true' #开启Tls服务
grafana['enable'] = false #关闭grafana服务
prometheus['enable'] = false #关闭普罗米修斯监控系统
gitlab_rails['backup_path'] = 备份路径 #修改默认备份文件路径
gitlab_rails['backup_keep_time'] = 秒 #修改备份文件保存时间
[root@gitlab /tmp]# egrep -v "^#|^$" /etc/gitlab/gitlab.rb 
external_url 'http://gitlab.aspen.com'
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'linux_edu@**.com'
gitlab_rails['gitlab_email_display_name'] = 'Linux_Edu'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.**.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "linux_edu"
gitlab_rails['smtp_password'] = "******"
gitlab_rails['smtp_domain'] = "**.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
prometheus['enable'] = false
grafana['enable'] = false

step3 初始化gitlab服务并启动gitlab服务

gitlab-ctl reconfigure #初始化gitlab服务
每次修改gitlab配置文件都需要重新初始化gitlab服务
[root@gitlab /tmp]# gitlab-ctl reconfigure
......
Running handlers:
Running handlers complete
Chef Client finished, 455/1192 resources updated in 01 minutes 52 seconds
gitlab Reconfigured!

step4 访问gitlab服务以及gitlab邮箱测试

默认登录用户名为"root"

相关命令

gitlab-ctl status #查看gitlab服务状态
gitlab-ctl stop 服务名称 #关闭 gitlab某项服务
gitlab-ctl stop #关闭gitlab服务
gitlab-ctl start 服务名称 #开启 gitlab某项服务
gitlab-ctl start #开启gitlab服务
gitlab-ctl tail -f #查看gitlab启动的后台日志
[root@gitlab /tmp]# gitlab-ctl stop node-exporter
ok: down: node-exporter: 0s, normally up
[root@gitlab /tmp]# gitlab-ctl stop postgres-exporter
ok: down: postgres-exporter: 0s, normally up
[root@gitlab /tmp]# gitlab-ctl status
run: alertmanager: (pid 12592) 1190s; run: log: (pid 12478) 1207s
run: gitaly: (pid 12537) 1192s; run: log: (pid 11982) 1280s
run: gitlab-monitor: (pid 12577) 1191s; run: log: (pid 12384) 1217s
run: gitlab-workhorse: (pid 12558) 1192s; run: log: (pid 12306) 1236s
run: logrotate: (pid 12324) 1230s; run: log: (pid 12334) 1229s
run: nginx: (pid 12299) 1236s; run: log: (pid 12317) 1233s
down: node-exporter: 17s, normally up; run: log: (pid 12365) 1221s
down: postgres-exporter: 4s, normally up; run: log: (pid 12520) 1201s
run: postgresql: (pid 12093) 1275s; run: log: (pid 12104) 1274s
run: redis: (pid 11943) 1282s; run: log: (pid 11972) 1281s
run: redis-exporter: (pid 12580) 1190s; run: log: (pid 12454) 1211s
run: sidekiq: (pid 12271) 1243s; run: log: (pid 12281) 1242s
run: unicorn: (pid 12243) 1249s; run: log: (pid 12267) 1248s

5.gitlab汉化

开始汉化之前,一定要启动gitlab服务并登录一次,保证服务完成目录加载。

step1 工具栏汉化

User->Settings->Preferences->Localization->Language->简体中文->Save changes

step2 内容视图汉化

  • 关闭gitlab服务
gitlab-ctl stop
[root@gitlab /tmp]# gitlab-ctl stop 
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 1s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: node-exporter: 327s, normally up
ok: down: postgres-exporter: 314s, normally up
ok: down: postgresql: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 1s, normally up
ok: down: unicorn: 0s, normally up
注:下载的汉化补丁版本要与gitlab版本对应
[root@gitlab /tmp]# ls *-zh.tar.gz
gitlab-12-0-stable-zh.tar.gz
  • 解压汉化补丁
tar xf 汉化补丁
[root@gitlab /tmp]# tar xf gitlab-12-0-stable-zh.tar.gz
[root@gitlab /tmp]# ll -d  gitlab-12-0-stable-zh
drwxrwxr-x 29 root root 4096 Jul 10 09:41 gitlab-12-0-stable-zh
[root@gitlab /tmp]# cat gitlab-12-0-stable-zh/VERSION 
12.0.3
  • 替换汉化资源包
    注:汉化之前,请先备份,防止因汉化失败导致服务崩溃
\cp -r 汉化补丁目录/* /opt/gitlab/embedded/service/gitlab-rails/
[root@gitlab /tmp]# \cp -r gitlab-v12.0.3-zh/* /opt/gitlab/embedded/service/gitlab-rails/
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/log’ with directory ‘gitlab-v12.0.3-zh/log’
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/tmp’ with directory ‘gitlab-v12.0.3-zh/tmp’

汉化(cp)过程中会提示两个错误,可以忽略错误,不影响汉化结果

  • 启动gitlab服务
gitlab-ctl start
[root@gitlab /tmp]# gitlab-ctl start 
ok: run: alertmanager: (pid 15099) 1s
ok: run: gitaly: (pid 15108) 0s
ok: run: gitlab-monitor: (pid 15129) 1s
ok: run: gitlab-workhorse: (pid 15132) 0s
ok: run: logrotate: (pid 15140) 1s
ok: run: nginx: (pid 15150) 0s
ok: run: node-exporter: (pid 15155) 1s
ok: run: postgres-exporter: (pid 15159) 0s
ok: run: postgresql: (pid 15240) 0s
ok: run: redis: (pid 15249) 1s
ok: run: redis-exporter: (pid 15253) 0s
ok: run: sidekiq: (pid 15258) 1s
ok: run: unicorn: (pid 15264) 0s

6.gitlab使用

关联用户

将系统的用户的ssh公钥上传至gitlab服务器的指定用户。

Windows使用gitlab

windows使用Gitlab需要借助gitbash工具
gitbash下载地址

注意事项

  1. 只要存在提交请求,就需先将本地库更新到最新,然后再提交请求
  2. 分支合并到master时,先切换至master并进行更新,再将master合并到分支测试无误后,最后将分支提交到本地仓库并更新到远程仓库

7.gitlab的备份

step1 配置备份路径以及备份周期(选做项)

配置文件
gitlab_rails['backup_path'] = 备份路径 #修改默认备份文件路径
gitlab_rails['backup_keep_time'] = 秒 #修改备份文件保存时间
[root@gitlab ~/treenb]# grep '^gitlab_rails'  /etc/gitlab/gitlab.rb |sed -n 4,5p
gitlab_rails['backup_path'] = "/backup/data/gitlab"
gitlab_rails['backup_keep_time'] = 259200

step2 创建对应的备份目录

mkdir -p 备份目录
[root@gitlab ~/treenb]# mkdir -p /backup/data/gitlab
[root@gitlab ~/treenb]# ls /backup/data/gitlab

step3 重新初始化gitlab

gitlab-ctl reconfigure
[root@gitlab ~/treenb]# gitlab-ctl reconfigure
......
Chef Client finished, 10/605 resources updated in 01 minutes 02 seconds
gitlab Reconfigured!

step4 执行备份命令

gitlab-rake gitlab:backup:create
执行该备份命令后,备份文件是自动存储到备份路径中
[root@gitlab ~/treenb]# gitlab-rake gitlab:backup:create
......
Backup task is done.
[root@gitlab ~/treenb]# ls -l /backup/data/gitlab/
total 152
-rw------- 1 git git 153600 Nov  1 20:27 1572611254_2019_11_01_12.0.3_gitlab_backup.tar

注:备份文件的文件名,不要改动;

step5 将备份命令加入定时任务(crond)

[root@gitlab ~/treenb]# crontab -l
#sync time by Aspen 20190511
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#Auto backup Gitlab by Aspen_Han 20191101
00 02 * * * /usr/bin/gitlab-rake gitlab:backup:create &>/dev/null

8.gitlab的恢复

step1 停止数据写入服务

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
[root@gitlab ~/treenb]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@gitlab ~/treenb]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

step2 根据备份文件时间戳执行恢复命令

gitlab-rake gitlab:backup:restore BACKUP=备份文件时间戳
[root@gitlab ~/treenb]# gitlab-rake gitlab:backup:restore BACKUP=1572611254_2019_11_01_12.0.3
Unpacking backup ... done
Before restoring the database, we will remove all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.

Do you want to continue (yes/no)? yes
......
Do you want to continue (yes/no)? yes
......
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need to restore these files manually.
Restore task is done.

step3 重启gitlab,检查恢复情况

gitlab-ctl restart
[root@gitlab ~/treenb]# gitlab-ctl restart
ok: run: alertmanager: (pid 32909) 0s
ok: run: gitaly: (pid 32918) 1s
ok: run: gitlab-monitor: (pid 32923) 0s
ok: run: gitlab-workhorse: (pid 32925) 1s
ok: run: logrotate: (pid 32932) 0s
ok: run: nginx: (pid 32939) 1s
ok: run: node-exporter: (pid 32941) 0s
ok: run: postgres-exporter: (pid 32959) 0s
ok: run: postgresql: (pid 32987) 0s
ok: run: redis: (pid 32991) 0s
ok: run: redis-exporter: (pid 33003) 1s
ok: run: sidekiq: (pid 33008) 0s
ok: run: unicorn: (pid 33014) 1s

9.gitlab的升级

step1 停止gitlab服务

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
[root@gitlab ~/treenb]#     gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@gitlab ~/treenb]#     gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@gitlab ~/treenb]#     gitlab-ctl stop nginx
ok: down: nginx: 0s, normally up

step2 备份gitlab

gitlab-rake gitlab:backup:create
[root@gitlab ~/treenb]# gitlab-rake gitlab:backup:create
......
Backup task is done.
[root@gitlab ~/treenb]# ll /backup/data/gitlab/
total 304
-rw------- 1 git git 153600 Nov  1 20:27 1572611254_2019_11_01_12.0.3_gitlab_backup.tar
-rw------- 1 git git 153600 Nov  1 20:53 1572612805_2019_11_01_12.0.3_gitlab_backup.tar
drwx------ 7 git git    131 Nov  1 20:43 tmp

step3 下载并安装高版本gitlab

yum localinstall -y 安装包
[root@gitlab /tmp]# yum localinstall -y gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
......
     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/

Upgrade complete! If your GitLab server is misbehaving try running
  sudo gitlab-ctl restart
before anything else.
If you need to roll back to the previous version you can use the database
backup made during the upgrade (scroll up for the filename).

  Verifying  : gitlab-ce-12.3.5-ce.0.el7.x86_64                                                     1/2 
  Verifying  : gitlab-ce-12.0.3-ce.0.el7.x86_64                                                     2/2 

Updated:
  gitlab-ce.x86_64 0:12.3.5-ce.0.el7                                                                    

Complete!

step4 重新初始化并重启gitlab

gitlab-ctl reconfigure
gitlab-ctl restart
[root@gitlab /tmp]# gitlab-ctl reconfigure
......
Warnings:
The version of the running postgresql service is different than what is installed.
Please restart postgresql to start the new version.

sudo gitlab-ctl restart postgresql

gitlab Reconfigured!
[root@gitlab /tmp]# gitlab-ctl restart
ok: run: alertmanager: (pid 36373) 1s
ok: run: gitaly: (pid 36382) 0s
ok: run: gitlab-exporter: (pid 36399) 1s
ok: run: gitlab-workhorse: (pid 36403) 0s
ok: run: logrotate: (pid 36418) 1s
ok: run: nginx: (pid 36424) 0s
ok: run: node-exporter: (pid 36429) 0s
ok: run: postgres-exporter: (pid 36433) 1s
ok: run: postgresql: (pid 36441) 0s
ok: run: redis: (pid 36527) 1s
ok: run: redis-exporter: (pid 36531) 0s
ok: run: sidekiq: (pid 36536) 1s
ok: run: unicorn: (pid 36542) 0s

step5 查看gitlab版本信息

head -1 /opt/gitlab/version-manifest.txt
[root@gitlab /tmp]# head -1 /opt/gitlab/version-manifest.txt
gitlab-ce 12.3.5

注:gitlab不能跨大版本直接升级,且要重新安装对应版本的汉化包

10.gitlab的迁移

gitlab的迁移必须保证新旧两台服务器上的gitlab版本一致

step1 迁移备份文件(默认恢复路径:/var/opt/gitlabbackups)
step2 迁移配置文件(注意调整数据存放目录)
step3 迁移nginx配置文件目录(默认路径:/var/opt/gitlab/nginx/conf)
step4 恢复配置文件
step5 初始化gitlab服务
step6 启动gitlab服务
step7 检测服务状态

11.gitlab更改默认Nginx

通常情况下,应有独立的服务器运行gitlab
直接在gitlab提供的nginx服务上面创建虚拟主机配置文件
[root@gitlab ~]# cd /var/opt/gitlab/nginx/conf/
[root@gitlab /var/opt/gitlab/nginx/conf]# ls
gitlab-http.conf  nginx.conf  nginx-status.conf
[root@gitlab /var/opt/gitlab/nginx/conf]# tail -3 nginx.conf 
  include /var/opt/gitlab/nginx/conf/nginx-status.conf;
  include /var/opt/gitlab/nginx/conf/host-test.conf;
}
[root@gitlab /var/opt/gitlab/nginx/conf]# vim host-test.conf
server {
    listen       80;
    server_name  www.aspen.com;

    location / {
        root   /var/html/www;
        index  index.html index.htm;
    }
}
[root@gitlab /var/opt/gitlab/nginx/conf]# mkdir -p /var/html/www
[root@gitlab /var/opt/gitlab/nginx/conf]# cd /var/html/www
[root@gitlab /var/html/www]# echo "Host_Page power by nginx of Gitlab" >index.html
[root@gitlab /var/html/www]# gitlab-ctl restart nginx
ok: run: nginx: (pid 38156) 0s
-----------------------------------------------------------------------------------------------------
[root@gitlab /var/html/www]# curl -H host:www.aspen.com 10.0.0.111
Host_Page power by nginx of Gitlab

附:思维导图

发表评论

您的电子邮箱地址不会被公开。