git_gitlab

本文作者:wuXing

QQ:1226032602

E-mail:[email protected]

git

https://git-scm.com/book/zh/v2

  1. Git诞生历史

我想大家还记得Linus torvalds在1991年时发布了Linux操作系统吧,从那以后Linux系统变不断发展壮大,因为Linux系统开源的特性,所以一直接受着来自全球Linux技术爱好者的贡献,志愿者们通过邮件向Linus发送着自己编写的源代码文件,然后由Linus本人通过手工的方式将代码合并,但这样不仅没有效率,而且真的是太痛苦了。

一直到2002年,Linux系统经过十余年的不断发展,代码库已经庞大到无法再让Linus通过手工的方式管理了,但是Linus真的很不喜欢CVS或者Subversion版本控制系统,于是商业公司BitMover决定将其公司的BitKeeper分布式版本控制系统授权给Linux开发社区来免费使用,当时的BitKeeper可以比较文件内容的不同,还能够将出错的文档还原到历史某个状态,Linus终于放下了心里的石头。

CVS和Subversion属于传统的版本控制系统,而分布式版本控制系统最大的特点是不需要每次提交都把文件推送到版本控制服务器,而是采用分布式版本库的机制,使得每个开发人员都够从服务器中克隆一份完整的版本库到自己计算机本地,不必再完全依赖于版本控制服务器,使得源代码的发布和合并更加方便,并且因为数据都在自己本地,不仅效率提高了,而且即便我们离开了网络依然可以执行提交文件、查看历史版本记录、创建分支等等操作,真的是开发者的福音啊。

就这样平静的度过了三年时间,但是Linux社区聚集着太多的黑客人物,2005年时,那位曾经开发Samba服务程序的Andrew因为试图破解BitKeeper软件协议而激怒了BitMover公司,当即决定不再向Linux社区提供免费的软件授权了,此时的Linus其实也早已有自己编写分布式版本控制系统的打算了,于是便用C语言花了2周创建了Git分布式版本控制系统,并上传了Linux系统的源代码。

Git不仅是一款开源的分布式版本控制系统,而且有其独特的功能特性,例如大多数的分布式版本控制系统只会记录每次文件的变化,说白了就是只会关心文件的内容变化差异,而Git则是关注于文件数据整体的变化,直接会将文件提交时的数据保存成快照,而非仅记录差异内容,并且使用SHA-1加密算法保证数据的完整性。

Git为了提高效率,对于没有被修改的文件,则不会重复存储,而是创建一个链接指向之前存储过的文件。

在正式使用前,我们还需要弄清楚Git的三种重要模式,分别是已提交、已修改、已暂存

已提交(committed):表示数据文件已经顺利提交到Git数据库中。

已修改(modified):表示数据文件已经被修改,但未被保存到Git数据库中。

已暂存(staged):表示数据文件已经被修改,并会在下次提交时提交到Git数据库中。

提交前的数据文件可能会被随意修改或丢失,但只要把文件快照顺利提交到Git数据库中,那就可以完全放心了,流程为:

1.在工作目录中修改数据文件。

2.将文件的快照放入暂存区域。

3.将暂存区域的文件快照提交到Git仓库中。

工作区域

git_gitlab_第1张图片

git_gitlab_第2张图片

  1. git安装部署

Git是分布式的版本控制系统,我们只要有了一个原始Git版本仓库,就可以让其他主机克隆走这个原始版本仓库,从而使得一个Git版本仓库可以被同时分布到不同的主机之上,并且每台主机的版本库都是一样的,没有主次之分,极大的保证了数据安全性,并使得用户能够自主选择向那个Git服务器推送文件了,其实部署一个git服务器是非常简单的。

git 10.0.0.200
jenkins 10.0.0.201

安装

yum install git

git编译安装

yum remove git
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y
yum install gcc perl-ExtUtils-MakeMaker -y
wget https://www.kernel.org/pub/software/scm/git/git-2.7.1.tar.gz
tar -xzvf git-2.7.1.tar.gz
cd git-2.7.1
make prefix=/usr/local/git all
make prefix=/usr/local/git install
ln -s /usr/local/git/bin/git /usr/bin/git

git配置

git config --global user.name "wuXing"
git config --global user.email "[email protected]"
git config --global color.ui true

git配置查看

git config --list
user.name=xiadongzhi
user.email=1226032602@qq.com
color.ui=true
cat ~/.gitconfig
[user]
name = xiadongzhi
email = 1226032602@qq.com
[color]
ui = true

git常用命令

add    #添加文件内容至索引
bisect      #通过二分查找定位引入 bug 的变更
branch       #列出、创建或删除分支
checkout      #检出一个分支或路径到工作区
clone        #克隆一个版本库到一个新目录
commit     #记录变更到版本库
diff         #显示提交之间、提交和工作区之间等的差异
fetch      #从另外一个版本库下载对象和引用
grep        #输出和模式匹配的行
init        #创建一个空的 Git 版本库或重新初始化一个已存在的版本库
log        #显示提交日志
merge     #合并两个或更多开发历史
mv        #移动或重命名一个文件、目录或符号链接
pull        #获取并合并另外的版本库或一个本地分支
push      #更新远程引用和相关的对象
rebase      #本地提交转移至更新后的上游分支中
reset       #重置当前HEAD到指定状态
rm         #从工作区和索引中删除文件
show     #显示各种类型的对象
status     #显示工作区状态
tag       #创建、列出、删除或校验一个GPG签名的 tag 对象

git基本操作

  1. 创建本地工作目录并进入目录
[root@centos72 ~]# mkdir git_data
[root@centos72 ~]# cd git_data/
  1. 初始化工作目录并查看状态
[root@centos72 git_data]# git init

ll -a .git/

branches # 分支目录
config   # 定义项目特有的配置选项
description  # 仅供git web程序使用
HEAD # 指示当前的分支
hooks # 包含git钩子文件
info # 包含一个全局排除文件(exclude文件)
objects # 存放所有数据内容,有info和pack两个子文件夹
refs # 存放指向数据(分支)的提交对象的指针
index # 保存暂存区信息,在执行git init的时候,这个文件还没有

初始化空的 Git 版本库于 /root/git_data/.git/

[root@centos72 git_data]# git status

位于分支 master

初始提交

无文件要提交(创建/拷贝文件并使用 “git add” 建立跟踪)

  1. git帮助
git help COMMAND

git四种状态

git_gitlab_第3张图片

  1. git添加文件到暂存区
[root@centos72 git_data]#**git add .**
  1. 提交暂存区文件至git**版本仓库**
[root@centos72 git_data]# git commit -m 'first commit'
[master(根提交) caa5b30] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
[root@centos72 git_data]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@centos72 git_data]# git add .
[root@centos72 git_data]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD ..." 撤出暂存区)
#
#       新文件:    database1
#
  1. 撤出暂存区(index)
git reset HEAD  filename  #add之后,未commit,可以撤出暂存区
  1. git修改文件名
[root@centos72 git_data]# git mv database1 database  #git修改文件名
[root@centos72 git_data]# git status

比较工作区与暂存区(diff)

git diff test.txt

比较暂存区与本地仓库

git diff --cached test.txt

查看提交历史记录(git log)

[root@centos72 git_data]# git log      #查看提交历史记录
commit caa5b30db4626b8c2f660be805d3c8d302d6e2b8
Author: xiadongzhi <1226032602@qq.com>

Date:   Thu Apr 13 20:10:00 2017 +0800

first commit
[root@centos72 git_data]# git commit -a -m 'three commit'
[master 09c2552] three commit
1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 database

[root@centos72 git_data]# git status
# 位于分支 master

无文件要提交,干净的工作区

git log

git log -2   #查看最近几条记录
git log -p -1#-p显示每次提交的内容差异,例如仅查看最近一次差异
git log –stat -2#–stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息

git log –pretty=oneline#–pretty根据不同的格式展示提交的历史信息

git log –pretty=fuller -2#以更详细的模式输出提交的历史记录
git log --oneline --decorate

还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有:

%s提交说明。

%cd提交日期。

%an作者的名字。

%cn提交者的姓名。

%ce提交者的电子邮件。

%H提交对象的完整SHA-1哈希字串。

%h提交对象的简短SHA-1哈希字串。

%T树对象的完整SHA-1哈希字串。

%t树对象的简短SHA-1哈希字串。

%P父对象的完整SHA-1哈希字串。

%p父对象的简短SHA-1哈希字串。

%ad作者的修订时间。

git log –pretty=fomat:”%h %cn”#查看当前所有提交记录的简短SHA-1哈希字串与提交着的姓名

查看所有日志git reflog

git reset --hard (回到指定的commit )

git log –-pretty=oneline  #提交的历史信息

Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本会叫HEAD,上上一个版本则会叫做HEAD^,当然一般会用HEAD~5来表示往上数第五个提交版本。

git reset –-hard HEAD^  #还原历史提交版本上一次

刚刚的操作实际上就是改变了一下HEAD版本指针的位置,就是你将HEAD指针放在那里,那么你的当前工作版本就会定位在那里,要想把内容再还原到最新提交的版本,先看查看下提交版本号

git reflog

112d84e HEAD@{0}: reset: moving to HEAD^
c6dca38 HEAD@{1}: commit: three commit

112d84e HEAD@{2}: commit: 2 commit

7b232d3 HEAD@{3}: commit (initial): first commit

git reset –-hard 4bf5b29#找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配)

git checkout – readme.txt#将文件内容从暂存区中恢复

这其中是有一套规则,如果暂存区中有该文件,则直接从暂存区恢复,如果暂存区没有该文件,则将还原成最近一次文件提交时的快照。

[root@centos7 git_data]# git log --pretty=oneline

c6dca38551445d19442ea7150c1aaced85cec596 three commit

112d84e10603ef43a2f338871960d688cfef76c5 2 commit

7b232d39230d50fea0ef3e713b3258b12b868d3a first commit

[root@centos7 git_data]#**git reset --hard HEAD^**

HEAD 现在位于 112d84e 2 commit

[root@centos7 git_data]# git log --pretty=oneline

112d84e10603ef43a2f338871960d688cfef76c5 2 commit

7b232d39230d50fea0ef3e713b3258b12b868d3a first commit
[root@centos7 git_data]# git reflog
112d84e HEAD@{0}: reset: moving to HEAD^
c6dca38 HEAD@{1}: commit: three commit

112d84e HEAD@{2}: commit: 2 commit

7b232d3 HEAD@{3}: commit (initial): first commit

[root@centos7 git_data]# git reset --hard c6dca38

HEAD 现在位于 c6dca38 three commit
git reset --hard HEAD@{3}

HEAD 现在位于 112d84e 2 commit

[root@centos7 git_data]# git reflog

112d84e HEAD@{0}: reset: moving to HEAD@{3}

c6dca38 HEAD@{1}: reset: moving to c6dca38

112d84e HEAD@{2}: reset: moving to HEAD^

c6dca38 HEAD@{3}: commit: three commit

112d84e HEAD@{4}: commit: 2 commit

7b232d3 HEAD@{5}: commit (initial): first commit
git reset --hard HEAD@{5}

HEAD 现在位于 7b232d3 first commit

[root@centos7 git_data]# echo ffkjfhflkjfs >> README

[root@centos7 git_data]# git checkout -- README      #  一键还原

[root@centos7 git_data]# cat README

[root@centos7 git_data]#

[root@centos7 git_data]# echo ffkjfhflkjfs >> README

[root@centos7 git_data]# git diff README

diff --git a/README b/README

index e69de29..eec6fa9 100644

--- a/README

+++ b/README

@@ -0,0 +1 @@

+ffkjfhflkjfs

git分支结构

分支结构介绍

分支即是平行空间,假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到git仓库中,又有可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫”拍照功能”的分支,这种分支只会属于你自己,而其他人看不到,等代码编写完成后再与原来的项目主分支合并下即可,这样即能保证代码不丢失,又不影响其他人的工作。

git_gitlab_第4张图片

一般在实际的项目开发中,我们要尽量保证master分支是非常稳定的,仅用于发布新版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后在合并到master分支上面,所以团队的合作分支看起来会像上面图那样

git_gitlab_第5张图片

HEAD指针指向当前分支,分支再指向版本

git log --oneline --decorate

一个完整的从创建分支到合并分支过程

查看当前分支情况,当前分支前有*号

git branch
* master

创建分支

git branch linux
[root@centos72 git_data]# git branch
linux
* master

删除分支

git branch -d testing

切换分支

git checkout linux   #切换分支
切换到分支 'linux'
[root@centos72 git_data]# git branch
* linux
master

写入新数据

[root@centos72 git_data]# echo "this is linux branch" >> README 
[root@centos72 git_data]# cat README 
this is linux branch
[root@centos72 git_data]# git status
# 位于分支 linux
# 尚未暂存以备提交的变更:
#   (使用 "git add ..." 更新要提交的内容)
#   (使用 "git checkout -- ..." 丢弃工作区的改动)
#
#       修改:      README
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a"

提交数据到仓库

[root@centos72 git_data]# git commit -a -m "linux branch first commit"
[linux c1eb951] linux branch first commit
 1 file changed, 1 insertion(+)
[root@centos72 git_data]# git status
# 位于分支 linux
无文件要提交,干净的工作区

切回master分支

[root@centos72 git_data]# git checkout master   #我们在提交文件后再切回master分支
切换到分支 'master'
[root@centos72 git_data]# cat README 
[root@centos72 git_data]#

合并Linux分支至master

将指定的分支与当前分支合并

git merge linux

git分支冲突

但是Git并不能每次都为我们自动的合并分支,当遇到了内容冲突比较复杂的情况,则必须手工将差异内容处理点,比如这样的情况

git checkout -b linux  #创建分支并切换到该分支
git branch #查看分支
* linux
master
vim readme.txt      #编辑readme文件
git add readme.txt     #在Linux分支添加readme至暂存区
git commit -m “create two”   #在Linux分支提交readme
[linux 13a42ad] create two
1 file changed, 1 insertion(+), 2 deletions(-)
git checkout master   #切换到master分支
Switched to branch ‘master’
git branch        #查看是否切换至master分支
linux
* master
vim readme.txt   #编在master分支上修改readme文件同一行的内容
git add readme.txt      #添加至暂存区
git commit -m ‘create to master’      #提交至Git版本仓库
[master 75bd55c] create to master
1 file changed, 1 insertion(+)
git merge linux     #合并Linux分支(冲突)
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
#那么此时,我们在master与linux分支上都分别对中readme文件进行了修改并提交了,那这种情况下Git就没法再为我们自动的快速合并了,它只能告诉我们readme文件的内容有冲突,需要手工处理冲突的内容后才能继续合并
cat readme.txt  #冲突内容如下
#Git用< <<<<<<,=======,>>>>>>>分割开了各个分支冲突的内容,我们需要手工的删除这些符号,并将内容修改
1 hehe
<<<<<<< HEAD
=======
>>>>>>> linux
Create new branch is linux
git add readme.txt      #再次添加至暂存区
git commit -m ‘config’  #提交至git版本仓库
[master eb9bb83] config
  1. 功能分支

创建一个功能分支:

git checkout -b feature-x develop

开发完成后,将功能分支合并到develop分支:

git checkout develop 
git merge --no-ff feature-x 

删除feature分支:

git branch -d feature-x
  1. Git 预发布分支

创建一个预发布分支:

git checkout -b release-1.2 develop

确认没有问题后,合并到master分支:

git checkout master

git merge --no-ff release-1.2

#对合并生成的新节点,做一个标签

git tag -a 1.2

再合并到develop分支:

git checkout develop

git merge --no-ff release-1.2

最后,删除预发布分支:

git branch -d release-1.2
  1. Git bug fix分支

创建一个修补bug分支:

git checkout -b fixbug-0.1 master

修补结束后,合并到master分支:

git checkout master

git merge --no-ff fixbug-0.1

git tag -a 0.1.1

再合并到develop分支:

git checkout develop

git merge --no-ff fixbug-0.1

最后,删除"修补bug分支":

git branch -d fixbug-0.1

git标签

标签对应commit对象

当版本仓库内的数据有个大的改善或者功能更新,我们经常会打一个类似于软件版本号的标签,这样通过标签就可以将版本库中的某个历史版本给记录下来,方便我们随时将特定历史时期的数据取出来用,另外打标签其实只是像某个历史版本做了一个指针,所以一般都是瞬间完成的

cd git_data/    #进入git版本控制系统
git tag v1.0    #当前提交内容打一个标签(方便快速回滚)
git tag         #查看当前所有的标签
v1.0
git show v1.0    #查看当前1.0版本的详细信息
git tag v1.2 -m “version 1.2 release is test”     #创建带有说明的标签,-a指定标签名字,-m指定说明文字
       #我们为同一个提交版本设置了两次标签,删除之前的v1.0
Deleted tag ‘v1.0’ (was 4bf5b29)
git tag          #再次查看,v1.0已经被删除
v1.2

创建标签

git tag v2.0 6cb579c

查看所有标签

git tag 

查看标签信息

git show v1.0

删除标签

git tag -d v1.0
[root@centos7 git_data]#**git reset --hard v201704132048**

HEAD 现在位于 c831de7 master branch yyyy  commit

[root@centos7 git_data]# ls

README

[root@centos7 git_data]# git tag v201704132048

[root@centos7 git_data]# git tag

v201704132048

[root@centos7 git_data]# git show v201704132048

commit c831de7f0e5cea57e4ccb7fc387bd1e94df6dc93

Merge: cbec1bb 82670f8

Author: zy <[email protected]>

Date:   Thu Apr 13 20:46:01 2017 +0800

master branch yyyy  commit

[root@centos7 git_data]# git log^C

[root@centos7 git_data]# touch dddd

[root@centos7 git_data]# git commit -a -m 'master branch zzzz  commit'

# 位于分支 master

# 未跟踪的文件:

#   (使用 "git add ..." 以包含要提交的内容)

#

# dddd

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

[root@centos7 git_data]# git add dddd

[root@centos7 git_data]# git commit -a -m 'master branch zzzz  commit'

[master 607d8ed] master branch zzzz  commit

1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 dddd

[root@centos7 git_data]# git show v201704132050

fatal: ambiguous argument 'v201704132050': unknown revision or path not in the working tree.

Use '--' to separate paths from revisions, like this:

'git  [...] -- [...]'

[root@centos7 git_data]# git tag v201704132050

[root@centos7 git_data]# git tag

v201704132048

v201704132050

[root@centos72 git_data]# git reset --hard v201704132048

git add .
git commit -m 'v1.1'
git push -u origin master 
git tag -a 'v1.1' -m 'v1.1'
git push -u origin v1.1 

本地公共仓库

创建仓库

mkdir git_data1
cd git_data1/
git init --bare
[root@git git_data1]# ll
total 12
drwxr-xr-x 2 root root   6 Aug 23 04:24 branches
-rw-r--r-- 1 root root  66 Aug 23 04:24 config
-rw-r--r-- 1 root root  73 Aug 23 04:24 description
-rw-r--r-- 1 root root  23 Aug 23 04:24 HEAD
drwxr-xr-x 2 root root 242 Aug 23 04:24 hooks
drwxr-xr-x 2 root root  21 Aug 23 04:24 info
drwxr-xr-x 4 root root  30 Aug 23 04:24 objects
drwxr-xr-x 4 root root  31 Aug 23 04:24 refs

克隆仓库

[root@git opt]# cd /tmp/
[root@git tmp]# git clone 192.168.0.176:/data/git_data1
Cloning into 'git_data1'...
The authenticity of host '192.168.0.176 (192.168.0.176)' can't be established.
ECDSA key fingerprint is SHA256:rmgoHqMJZJ7mgGAUbzv5nQkwJSZsguzpDcLYJQ9wbic.
ECDSA key fingerprint is MD5:41:fd:a4:03:1d:fe:e0:59:25:b5:66:94:e9:57:98:13.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.176' (ECDSA) to the list of known hosts.
[email protected]'s password: 
warning: You appear to have cloned an empty repository.
[root@git tmp]# ll git_data1/
total 0
[root@git tmp]# ll git_data1/ -a
total 0
drwxr-xr-x  3 root root  18 Aug 23 04:30 .
drwxrwxrwt. 9 root root 138 Aug 23 04:30 ..
drwxr-xr-x  7 root root 119 Aug 23 04:30 .git           
[root@git tmp]# cd git_data1/
[root@git git_data1]# ll
total 0
[root@git git_data1]# touch test.txt
[root@git git_data1]# git add .
[root@git git_data1]# git commit -m 'add test.txt'
[master (root-commit) abeb9ac] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@git git_data1]# 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 matchingTo squelch this message and adopt the new behavior now, use:  git config --global push.default simpleSee '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)[email protected]'s password: 
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to '192.168.0.176:/data/git_data1'
[root@git git_data1]# git config --global push.default matching
[root@git git_data1]# git config --global push.default simple
[root@git git_data1]# git push 
[email protected]'s password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 192.168.0.176:/data/git_data1
 * [new branch]      master -> master

gitlab

Gitlab的服务构成

  • nginx: 静态web服务器 gitlab‐workhorse: 轻量级的反向代理服务器 logrotate:日志文件管理工具

  • postgresql:数据库

  • redis:缓存数据库

  • sidekiq:用于在后台执行队列任务(异步执行)。(Ruby)

  • unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。(Ruby Web Server,主要使用Ruby编写)

  1. gitlab安装
yum install curl policycoreutils  postfix -y
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce

cat /etc/yum.repos.d/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
yum makecache
yum install gitlab-ce -y

yum -y localinstall gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
  1. 设置URL地址

vim /etc/gitlab/gitlab.rb

external_url 'http://10.0.0.200'
...
external_url 'http://192.168.66.100:82'
...
nginx['listen_port'] = 82
  1. 初始化&&修改配置后执行
gitlab-ctl reconfigure

管理命令

gitlab-ctl status
gitlab-ctl stop
gitlab-ctl start
gitlab‐ctl stop nginx # 单独停止某个服务
gitlab‐ctl tail # 查看所有服务的日志
systemctl enable postfix

gitlab版本查看

cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

14.x.x版本初始化之后密码存放位置

...
Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.

NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
...

重置密码

gitlab-rake "gitlab:password:reset[root]"
Enter password: 
Confirm password: 
Password successfully updated for user with username root.

gitlab集成ldap

gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
  main: # 'main' is the GitLab 'provider ID' of this LDAP server
    label: 'LDAP'
    host: '192.168.44.20'
    port: 389
    uid: 'uid'
    bind_dn: 'cn=writeuser,cn=manager,dc=xxxxxx,dc=com,dc=cn'
    password: 'password123'
    encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
    verify_certificates: false
    smartcard_auth: false
    active_directory: false
    allow_username_or_email_login: false
    lowercase_usernames: false
    block_auto_created_users: false
    base: 'dc=xxxxxx,dc=com,dc=cn'
    user_filter: ''
    attributes:
      username: ['uid', 'userid', 'sAMAccountName']
      email:    ['mail', 'email', 'userPrincipalName']
      name:       'displayName'
      first_name: 'cn'
      last_name:  'sn'
EOS

浏览器访问

通过浏览器访问页面, http://10.0.0.200 设置初始密码,其他操作类似GitHUB。

账户:root密码自己设置为12345678

/opt/gitlab/ # gitlab的程序安装目录

/var/opt/gitlab # gitlab目录数据目录

/var/opt/gitlab/git‐data # 存放仓库数据

设置logo

git_gitlab_第6张图片

  1. 生成sshkey秘钥对并上传公钥
ssh-keygen -t rsa
cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLi+JwZtzvRsiEPX/9b0gtQkgC8cyRcOV7M2sBl1Z2TH8jINbD5yKIlIPRtcbtDAJIcPp+wbmR+Pv3BTL9R3GiVNLzqDGyy3t/2eoGxMeQZ9+BQX7gE5HdXLossHH6IryRK3eMi4TroVKqqkYgq/7CWwzogDNevUoc7tLHOCEwcfe+sW5wCrlEy9wjkTunrXHrbZlAYPnbOOH7K4XbdDZy0M2spLHGm1iFSI8xgHFJXdZIDhsKmPPDKGGPsaMY6F77mLsdCZkHLpnbbtCFDarkupoR2E4QVXi+8jPOcfeZSiK7nhtyiQI/fvUz+BYV1tjKkFEosj0AVeBY1fED9P0d root@db01

Command line instructions

Git global setup
git config --global user.name "Administrator"
git config --global user.email "[email protected]"

Create a new repository

git clone [email protected]:oldboy/git_data.git
cd git_data
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin [email protected]:oldboy/git_data.git
git add .
git commit -m "Initial commit"
git push -u origin master

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin [email protected]:oldboy/git_data.git
git push -u origin --all
git push -u origin --tags
  1. git全局配置(客户端)
git config --global user.name "dongzhi.xia"
git config --global user.email "[email protected]"
git config --global color.ui true    #语法高亮
git config --list
  1. 下载代码到本地
git clone [email protected]:root/git_test.git
cd git_test
touch README.md
git add .
git commit -m "add README"
git push -u origin master
  1. 创建一个新文件
[root@db01 oldboy_linux]# touch git.sh
[root@db01 oldboy_linux]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add ..." 以包含要提交的内容)
#
# git.sh
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
  1. 提交到暂存区
[root@db01 oldboy_linux]# git add git.sh
[root@db01 oldboy_linux]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD ..." 撤出暂存区)
#
# 新文件:    git.sh
#
  1. 提交暂存区文件到git版本仓库
[root@db01 oldboy_linux]# git commit -m "add git.sh"
[master 93ad3f7] add git.sh
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 git.sh
[root@db01 oldboy_linux]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
  1. 代码仓库推送到gitlab
git config --global push.default simple
[root@db01 oldboy_linux]# git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 241 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To [email protected]:oldboy/oldboy_linux.git
   9ff809c..93ad3f7  master -> master
[root@db01 oldboy_linux]# git status
# 位于分支 master
无文件要提交,干净的工作区
  1. 把gitlab代码拉到本地仓库
git pull
  1. git remote
git remote rename origin old-origin
git remote add origin [email protected]:root/clsn.git
git push -u origin --all

合并请求

git_gitlab_第7张图片

master分支保护

git_gitlab_第8张图片

dev分支操作

git branch dev
git checkout dev 
touch dev
git add .
git commit ‐m "add dev"
git push ‐u origin dev

GitLab修改root用户密码

http://docs.gitlab.com/ce/security/reset_root_password.html

[root@svr34 bin]# gitlab-rails console production
Loading production environment (Rails 4.2.5.2)
irb(main):001:0> user = User.where(id: 1).first
=> #
irb(main):002:0> user.password=12345678
=> 12345678
irb(main):003:0> user.password_confirmation=12345678
=> 12345678
irb(main):004:0> user.save!
=> true
irb(main):005:0> quit

ssh端口不是默认端口

echo  'port 12345' > ~/.ssh/config
chmod 600 ~/.ssh/config

gitlab备份恢复

gitlab备份

gitlab数据目录

/var/opt/gitlab

vim /etc/gitlab/gitlab.rb

#最后添加

gitlab_rails['backup_path'] = '/data/backup/gitlab'
gitlab_rails['backup_keep_time'] = 604800
gitlab-ctl reconfigure
gitlab-ctl restart

备份命令

gitlab-rake gitlab:backup:create

恢复

停止写入

gitlab-ctl stop unicorn
gitlab-ctl stop sideki
gitlab-ctl stop sidekiq

恢复命令

cd /data/backup/gitlab
gitlab-rake gitlab:backup:restore BACKUP=此处写备份文件的数字部分
gitlab-rake gitlab:backup:restore BACKUP=1541213673_2018_11_03_10.2.2

重启

gitlab-ctl restart

升级

import project


github

https://github.com/zhangyao8/zabbix-community-repos

https://monitoringartist.github.io/zabbix-searcher/

http://git.oschina.net/progit/

https://www.gitbook.com/book/bingohuang/progit2/details

https://docs.gitlab.com/ee/README.html

https://docs.gitlab.com/omnibus/README.html

每次操作前 git pull 操作后 git push

…or create a new repository on the command line
echo "# git_datas" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:xiadongzhi1988/git_datas.git
git push -u origin master

…or push an existing repository from the command line

git remote add origin [email protected]:xiadongzhi1988/git_datas.git
git push -u origin master

创建远程仓库

git remote add origin [email protected]:xiadongzhi1988/git_data.git

推送本地master

git push -u origin master

查看远程仓库

git remote

fetch拉取远程仓库到本地仓库

[root@ci-node1 git_test]# git fetch 
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From github.com:xiadongzhi1988/git_test
   4c2ef0f..4eba79f  master     -> github/master

合并

[root@ci-node1 git_test]# git merge github/master

推送到远程仓库

git push

你可能感兴趣的:(gitlab,jenkins,nginx,centos,linux)