22.1 代码管理平台介绍

  • 版本控制,记录若干文件内容变化,以便将来查阅特定版本修订情况

版本管理工具发展简史,cvs—> svn—> git  参考http://luckypoem14.github.io/test/2012/04/24/scm-history/

  • svn全称subversion,是一个开源版本控制系统,始于2000年

  • git是linux创始人linus发起的,2005年发布,最初目的是更好管理linux内核代码

  • git和svn不同在于git不需要依赖服务端就可以工作,即git是分布式的

  • 关于git和svn的比较大家参考http://blog.lishiming.net/?p=305

  • github是基于git的在线web页面代码托管平台,可以选择付费服务

  • gitlab可以认为是一个开源的github,两者没有直接关系


22.2 安装svn

1、安装服务端

[root@arslinux-01 ~]# yum install -y subversion

2、创建版本库

[root@arslinux-01 ~]# mkdir -p /data/svnroot/myproject
[root@arslinux-01 ~]# svnadmin create /data/svnroot/myproject/
[root@arslinux-01 ~]# cd /data/svnroot/myproject/conf/
[root@arslinux-01 conf]# ls
authz  passwd  svnserve.conf
[root@arslinux-01 conf]# vim authz
admins = arslinux,user1
[/]
@admins = rw
*= r
[myproject:/]
user1 = rw

[/] /指的就是/data/svnroot/myproject/,在这个目录下 admin 组权限时 rw

* 表示剩余的人是 r 权限

[myproject:/] 一般用在 svnroot 下有多个项目的情况下,其中一个 myproject 的项目

可以在这个目录下创建子目录 / 前面 : 表示 myproject 是个项目名


3、设置密码

[root@arslinux-01 conf]# vim passwd
[users]
arslinux = arslinux_!(*$123
user1 = user1_^^^123
user2 = user2-***123

4、编辑配置文件 svnserver.conf

[root@arslinux-01 conf]# vim svnserve.conf
[general]
anon-access = none                //匿名用户有无权限
auth-access = write                //被授权用户 可写
password-db = passwd                //用户密码存在哪里
authz-db = authz                    //权限控制文件
realm = /data/svnroot/myproject    //对哪个项目生效

5、启动 svn

[root@arslinux-01 conf]# svnserve -d -r /data/svnroot/

-d 后台启动

-r 指定项目所在路径,也可写成 svnserve -d -r /data/svnroot/myproject/

[root@arslinux-01 conf]# ps aux|grep svnserve
root      31030  0.0  0.0 180732   804 ?        Ss   22:24   0:00 svnserve -d -r /data/svnroot/
root      31060  0.0  0.0 112724   988 pts/1    S+   22:26   0:00 grep --color=auto svnserve
[root@arslinux-01 conf]# netstat -lntp|grep svnserve
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      31030/svnserve


22.3 客户端上使用svn(linux)

1、客户端安装 subversion

[root@arslinux-02 ~]# yum install -y subversion

2、连接 svn 服务器(本地可以保存用户名和密码)

[root@arslinux-02 ~]# cd /home/
[root@arslinux-02 home]# svn checkout svn://192.168.194.130/myproject --username=user1
认证领域: <svn://192.168.194.130:3690> /data/svnroot/myproject
“user1”的密码:

-----------------------------------------------------------------------
注意!  你的密码,对于认证域:

<svn://192.168.194.130:3690> /data/svnroot/myproject

只能明文保存在磁盘上!  如果可能的话,请考虑配置你的系统,让 Subversion
可以保存加密后的密码。请参阅文档以获得详细信息。

你可以通过在“/root/.subversion/servers”中设置选项“store-plaintext-passwords”为“yes”或“no”,
来避免再次出现此警告。
-----------------------------------------------------------------------
保存未加密的密码(yes/no)?yes
取出版本 0。

3、 拷贝任意一个文件到项目中

[root@arslinux-02 home]# cd myproject/
[root@arslinux-02 myproject]# cp /etc/fstab ..

4、添加到版本控制中心  svn add .

[root@arslinux-02 myproject]# svn add ./fstab
A         fstab

5、把文件上传到服务器 svn commit -m "add file"

[root@arslinux-02 myproject]# svn commit -m "add fstab"
正在增加       fstab
传输文件数据.
提交后的版本为 1。

6、在服务端上升级版本才能看见上传的文件

把当前目录下的文件都更新到最新版  svn update 可简写为 svn up

[root@arslinux-01 svntest]# cd myproject/
[root@arslinux-01 myproject]# ls
[root@arslinux-01 myproject]# svn up
正在升级 '.':
A    fstab
更新到版本 1。
[root@arslinux-01 myproject]# ls
fstab

7、用户名密码被记录在 /root/.subversion/auth/svn.simple/ 下

[root@arslinux-02 .subversion]# cat /root/.subversion/auth/svn.simple/7681a1260e61438030ce66c9a673594d
K 8
passtype
V 6
simple
K 8
password
V 12
user1_^^^123
K 15
svn:realmstring
V 52
<svn://192.168.194.130:3690> /data/svnroot/myproject
K 8
username
V 5
user1
END

8、如果不想用户信息被记录,可以删除 svn.simple 下的文件即可

[root@arslinux-01 svn.simple]# rm -rf /root/.subversion/auth/svn.simple/7681a1260e61438030ce66c9a673594d

9、在客户端更改文件,添加任意一行到 fstab,并将更改数据上传服务端

[root@arslinux-02 myproject]# echo '"xafdasfasdfsa" >> fstab
[root@arslinux-02 myproject]# svn commit -m "ch fstab"
正在发送       fstab
传输文件数据.
提交后的版本为 2。

10、服务端再更新版本,发现需要密码(提示输入 root 密码,可回车跳过,输入实际用户名密码)

[root@arslinux-01 myproject]# svn up
正在升级 '.':
认证领域: <svn://192.168.194.130:3690> /data/svnroot/myproject
“root”的密码:
认证领域: <svn://192.168.194.130:3690> /data/svnroot/myproject
用户名: arslinux
“arslinux”的密码:

-----------------------------------------------------------------------
注意!  你的密码,对于认证域:

<svn://192.168.194.130:3690> /data/svnroot/myproject

只能明文保存在磁盘上!  如果可能的话,请考虑配置你的系统,让 Subversion
可以保存加密后的密码。请参阅文档以获得详细信息。

你可以通过在“/root/.subversion/servers”中设置选项“store-plaintext-passwords”为“yes”或“no”,
来避免再次出现此警告。
-----------------------------------------------------------------------
保存未加密的密码(yes/no)?yes
U    fstab
更新到版本 2。

11、文件最后一行也增加了刚才输入的随机字符串

[root@arslinux-01 myproject]# tail -1 fstab
xafdasfasdfsa

12、在服务端本地删除文件,服务器上删除文件

[root@arslinux-01 myproject]# svn delete fstab
D         fstab
[root@arslinux-01 myproject]# svn commit -m "delete fstab"
正在删除       fstab
提交后的版本为 3。

13、在客户端上更新同步

[root@arslinux-02 myproject]# svn up
正在升级 '.':
D    fstab
更新到版本 3。
[root@arslinux-02 myproject]# ls

14、查看变更历史  svn log

[root@arslinux-02 myproject]# svn log
------------------------------------------------------------------------
r3 | arslinux | 2019-07-22 22:06:45 +0800 (一, 2019-07-22) | 1 行

delete fstab
------------------------------------------------------------------------
r2 | user1 | 2019-07-22 21:43:42 +0800 (一, 2019-07-22) | 1 行

ch fstab
------------------------------------------------------------------------
r1 | user1 | 2019-07-22 21:22:41 +0800 (一, 2019-07-22) | 1 行

add fstab

svn: E155015: 提交失败(细节如下) 解决办法:

svn resolved <文件名>


22.4 客户端上使用svn(windows)

  • 下载 TortoiseSVN 并安装 https://tortoisesvn.net/index.zh.html

1、安装完软件,再安装语言包即可

2、硬盘里新建 myproject 文件夹

3、右击 myproject 文件夹,选择 SVN Checkout...

4、填入项目的服务地址

【0718】 代码管理平台_分支

5、点 OK 后,输入用户名和密码

【0718】 代码管理平台_svn_02【0718】 代码管理平台_分支_03

5、在 myproject 文件夹下新建 123.txt,内容随便

6、右击 123.txt,选择 TortoiseSVN——Add

7、右击 123.txt,SVN Commit...

8、在弹框中做一些说明

【0718】 代码管理平台_GitHub_04

【0718】 代码管理平台_GitHub_05

9、在 linux 服务端机器

[root@arslinux-01 myproject]# svn up
正在升级 '.':
A    123.txt
更新到版本 6。
[root@arslinux-01 myproject]# cat 123.txt
adsfadfasdfa

10、在另一个linux客户端的机器上编辑一个文件并上传

[root@arslinux-02 myproject]# echo "adfadfafa\nadsfdafa" > ars4life
[root@arslinux-02 myproject]# cat ars4life
adfadfafa\nadsfdafa
[root@arslinux-02 myproject]# svn add ars4life
A         ars4life
[root@arslinux-02 myproject]# svn commit -m "add asr4life"
正在增加       ars4life
传输文件数据.
提交后的版本为 7。

11、然后在 windows 机器上,右击 myproject ,选择 SVN Update,myproject 目录里会有 ars4life 文件

简明教程http://www.jianshu.com/p/6b3b7b915332


22.5/22.6 单机上使用git

1、安装

[root@arslinux-01 ~]# yum install -y git

2、创建 gitroot目录

[root@arslinux-01 ~]# mkdir /data/gitroot/
[root@arslinux-01 ~]# cd !$
cd /data/gitroot/
[root@arslinux-01 gitroot]#

3、初始化仓库

[root@arslinux-01 gitroot]# git init
初始化空的 Git 版本库于 /data/gitroot/.git/
[root@arslinux-01 gitroot]# ll -a
总用量 0
drwxrwxr-x  3 root root  18 7月  23 22:18 .
drwxr-xr-x 11 root root 135 7月  23 22:18 ..
drwxrwxr-x  7 root root 119 7月  23 22:18 .git

4.在库中创建一个文件,任意内容

[root@arslinux-01 gitroot]# echo "dasfdafasfafas" > 1.txt
[root@arslinux-01 gitroot]# echo "ddfdfdfw22ghghg" >> 1.txt
[root@arslinux-01 gitroot]# echo "dftyuoiuoryfhgjg" >> 1.txt

5、将文件添加到仓库(方法几乎和 svn 一致)

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"

*** Please tell me who you are.

Run

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

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@arslinux-01.(none)')

6、上传时出错,根据参考内容,设置邮箱和用户名

[root@arslinux-01 gitroot]# git config --global user.email "zy77xp2316@qq.com"
[root@arslinux-01 gitroot]# git config --global user.name "arslinux"

也可以去 /root/.gitconfig 下更改

7、重新做上传

[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master(根提交) f04992b] add 1.txt
1 file changed, 3 insertions(+)
create mode 100644 1.txt

8、更改 1.txt 内容

[root@arslinux-01 gitroot]# echo "888888" >> 1.txt
[root@arslinux-01 gitroot]# echo "777777" >> 1.txt

9、修改的内容如果提交上传,还需要操作 git add 和 git commit -a

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 03d2ada] add 1.txt
1 file changed, 2 insertions(+)

10、查看当前仓库中的状态,比如是否有改动的文件

[root@arslinux-01 gitroot]# git status
# 位于分支 master
无文件要提交,干净的工作区

11、如果没有 git add 和 git commit 的步骤,会提示

[root@arslinux-01 gitroot]# echo " dasfadsf a" >> 1.txt
[root@arslinux-01 gitroot]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#修改:      1.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

12、对比修改了哪些内容可以用 git diff

[root@arslinux-01 gitroot]# git diff
diff --git a/1.txt b/1.txt
index 03086e7..e51c67f 100644
--- a/1.txt
+++ b/1.txt
@@ -3,3 +3,4 @@ ddfdfdfw22ghghg
dftyuoiuoryfhgjg
888888
777777
+ dasfadsf a

13、再做几次操作,查看所有提交记录

[root@arslinux-01 gitroot]# vim 1.txt
增加一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 3517c53] add 1.txt
1 file changed, 2 insertions(+)
[root@arslinux-01 gitroot]# vim 1.txt
删除一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "ch 1.txt"
[master b68b8fb] ch 1.txt
1 file changed, 1 deletion(-)
  • 查看提交记录

[root@arslinux-01 gitroot]# git log
commit b68b8fb900160e2ab06b4204d97f659fa14fa26a
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:45:24 2019 +0800
ch 1.txt
commit 3517c53c22f14c345206324bf9d190f157ec1f83
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:44:55 2019 +0800
add 1.txt
commit 03d2adacc5ee700de75799b90c1534ad6d9e6b9a
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:36:19 2019 +0800
add 1.txt
commit f04992b2e67da3ac3bfbc1c6a21053312f27ac76
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:29:07 2019 +0800
add 1.txt
  • 只显示一行信息  git log --pretty=oneline

[root@arslinux-01 gitroot]# git log --pretty=oneline
b68b8fb900160e2ab06b4204d97f659fa14fa26a ch 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt

14、git reset --hard 字符串  回退版本,其中后面跟的字符串是简写  

[root@arslinux-01 gitroot]# git reset --hard 3517c5
HEAD 现在位于 3517c53 add 1.txt
[root@arslinux-01 gitroot]# git log --pretty=oneline
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt

15、如果回退出了错,现在想回到 b68b8fb900 的版本

可以直接 git reset --hard b68b8fb900,也可以使用 git reflog

[root@arslinux-01 gitroot]# git reflog
3517c53 HEAD@{0}: reset: moving to 3517c5
b68b8fb HEAD@{1}: commit: ch 1.txt
3517c53 HEAD@{2}: commit: add 1.txt
03d2ada HEAD@{3}: commit: add 1.txt
f04992b HEAD@{4}: commit (initial): add 1.txt

然后在使用 git reset --hard b68b8fb

16、如果不小心删除了文件,那么用 git checkout -- 文件名 来恢复

[root@arslinux-01 gitroot]# rm -rf 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
[root@arslinux-01 gitroot]# ls
1.txt

17、如果文件本修改,add 后没有 commit,想回到上一次提交的状态,可以使用 git reset HEAD 文件名,然后在执行git checkout -- 文件名

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git reset HEAD 1.txt
重置后撤出暂存区的变更:
M1.txt
[root@arslinux-01 gitroot]# git checkout -- 1.txt

两个步骤:

1)git reset HEAD 文件名 从缓存区撤销

2)git checkout -- 文件名 恢复成修改前的状态

18、删除文件

[root@arslinux-01 gitroot]# git rm 1.txt
rm '1.txt'
[root@arslinux-01 gitroot]# git commit -m "delete 1.txt"
[master 560134a] delete 1.txt
1 file changed, 7 deletions(-)
delete mode 100644 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
error: pathspec '1.txt' did not match any file(s) known to git.

19、文件删除后依然可以找回,用 git reset 找回

[root@arslinux-01 gitroot]# git log --pretty=oneline
560134a1708b3815cb53af6ae160240b574e2240 delete 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt
[root@arslinux-01 gitroot]# git reset --hard 3517c53c22f1
HEAD 现在位于 3517c53 add 1.txt
[root@arslinux-01 gitroot]# ls
1.txt


22.7 建立远程仓库

1、首先到 https://github.com 注册一个账号,创建自己的git,点右上角 “+”,选择 New repository

2、仓库名字自定义,比如叫,可以添加注释(Description),权限选择public,点Create repository

3、添加key:右上角点自己头像,选择settings,左侧选择SSH and GPG keys

4、右侧点 New SSH key,把linux机器上的 ~/.ssh/id_rsa.pub 内容粘贴到这里,可以用 ssh-keygen 生成

5、在本地创建一个仓库并进入

[root@arslinux-01 ~]# mkdir /tmp/studygit/
[root@arslinux-01 ~]# cd /tmp/studygit/

6、根据提示进行操作,首先创建一个 README.md

[root@arslinux-01 studygit]# echo "# studygit" >> README.md

7、初始化,生成 .git

[root@arslinux-01 studygit]# git init
初始化空的 Git 版本库于 /tmp/studygit/.git/
[root@arslinux-01 studygit]# ll -a
总用量 4
drwxrwxr-x  3 root root  80 7月  25 22:05 .
drwxrwxrwt 11 root root 340 7月  25 22:05 ..
drwxrwxr-x  7 root root 200 7月  25 22:05 .git
-rw-rw-r--  1 root root  11 7月  25 22:04 README.md

8、提交 README.md 到仓库

[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "first commit"
[master(根提交) f6afa16] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

9、将仓库内容推到远程

[root@arslinux-01 studygit]# git remote add origin https://github.com/axxxxx4xxx/studygit.git

10、将更改推送到远程

[root@arslinux-01 studygit]# git push -u origin master
Username for 'https://github.com': axxxxx4xxx
Password for 'https://axxxxx4xxx@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxx/studygit.git
* [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

11、推送新增文件操作

[root@arslinux-01 studygit]# echo "73737372hhdjdjd" > 2.txt
[root@arslinux-01 studygit]# ls
2.txt  README.md
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[master 6e4549c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 studygit]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
f6afa16..6e4549c  master -> master

12、根据提示,设置一下 git config

[root@arslinux-01 studygit]# git config --global push.default simple

13、网页刷新,就可以看到新增的文件

【0718】 代码管理平台_svn_06

22.8 克隆远程仓库

1、网页端仓库中,点右侧绿色“Clone or download”,复制生成的 git 链接

2、克隆到本机

[root@arslinux-01 studygit]# cd
[root@arslinux-01 ~]# cd /home/
[root@arslinux-01 home]# git clone https://github.com/arsenal4life/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
[root@arslinux-01 home]# cd studygit/
[root@arslinux-01 studygit]# ls
2.txt  README.md

3、更改 README.md 内容,并推送到远程

[root@arslinux-01 studygit]# echo "hello hello" >> README.md
[root@arslinux-01 studygit]# echo "bye" >> README.md
[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "change README.md"
[master b2c0aa1] change README.md
1 file changed, 2 insertions(+)
[root@arslinux-01 studygit]# git push
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
6e4549c..b2c0aa1  master -> master

4、成功

【0718】 代码管理平台_分支_07

5、将远程更改文件同步到本地

——远程修改 2.txt 内容

[root@arslinux-01 studygit]# git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/arsenal4life/studygit
b2c0aa1..cce2494  master     -> origin/master
更新 b2c0aa1..cce2494
Fast-forward
2.txt | 3 +++
1 file changed, 3 insertions(+)
[root@arslinux-01 studygit]# cat 2.txt
73737372hhdjdjd
thank you
go go go
ore wa neko ga suki


22.9 分支管理

22.9 分支管理

  • git branch                   查看分支

  • git branch 分支名          创建分支

  • git checkout 分支名       切换分支

[root@arslinux-01 gitroot]# git branch
* master
[root@arslinux-01 gitroot]# git branch arslinux
[root@arslinux-01 gitroot]# git branch
arslinux
* master
[root@arslinux-01 gitroot]# git checkout arslinux
切换到分支 'arslinux'
[root@arslinux-01 gitroot]# git branch
* arslinux
master

——当前使用的分支前面会有一个 * 在 arslinux 分支下

——编辑 2.txt,并提交到新分支

[root@arslinux-01 gitroot]# echo "kajlhlk" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "add 2.txt"
[arslinux 0f6b36c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt  2.txt
[root@arslinux-01 gitroot]# git checkout master
切换到分支 'master'
[root@arslinux-01 gitroot]# ls
1.txt

分支可以隔离开文件

  • git merge 分支名          合并分支

[root@arslinux-01 gitroot]# git merge arslinux
更新 3517c53..0f6b36c
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt  2.txt

想要将文件合并到哪个分支下,那么在合并前,需要先切换到该分支下,再进行合并分支操作

以上操作中,合并分之后,master 分支下有了 2.txt

——合并分支时,如果合并的分支下有相同文件名的文件,那么文件内容需要一致

[root@arslinux-01 gitroot]# echo "12sddsf2232" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[master bb88c13] ch 2.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout arslinux
切换到分支 'arslinux'
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[arslinux 79c2a72] ch 2.txt
1 file changed, 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout master
切换到分支 'master'
[root@arslinux-01 gitroot]# git merge arslinux
自动合并 2.txt
冲突(内容):合并冲突于 2.txt
自动合并失败,修正冲突然后提交修正的结果。
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# cat 2.txt
<<<<<<< HEAD
12sddsf2232
=======
>>>>>>> arslinux
  • 如果master分支和aming分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。

  • 解决冲突的方法是在master分支下,编辑2.txt,改为aming分支里面2.txt的内容。 然后提交2.txt,再合并aming分支。

  • 但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑2.txt内容,改为想要的,然后提交。切换到aming分支,然后合并master分支到aming分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。


  • git branch -d 分支名          删除分支

  • git branch -D arslinux      强制删除分支

[root@arslinux-01 gitroot]# git branch -d arslinux
已删除分支 arslinux(曾为 fcb0316)。

如果分支没有合并,删除之前会提示,那就不合并,强制删除


22.10 远程分支管理

使用分支的原则:

  • master 分支非常重要,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。

  • 创建一个 dev 分支,专门用作开发,当发布到线上之前,才会把 dev 分支合并到 master

  • 开发人员应该在 dev 的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到 dev 分支

【0718】 代码管理平台_git_08

dev分支合并bob分支的命令是:

git checkout dev   //先切换到dev分支,然后

git merge bob


远程分支:

  • git ls-remote origin          查看远程分支

[root@arslinux-01 tmp]# git clone https://github.com/axxxxxx4xxxx/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 15 (delta 0), reused 12 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
[root@arslinux-01 tmp]# cd studygit/
[root@arslinux-01 studygit]# git branch
* master
[root@arslinux-01 studygit]# git ls-remote origin
38e08903596878b892452d53aa96dda7b76a7c64HEAD
38e08903596878b892452d53aa96dda7b76a7c64refs/heads/dev
38e08903596878b892452d53aa96dda7b76a7c64refs/heads/master

——本地新建的分支如果不推送到远程,对其他人就是不可见的

——git clone 的时候默认只把 master 分支克隆下来,如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用 git checkout -b 分支名 origin/分支名,本地和远程分支的名称要一致

  • git checkout -b 分支名 origin/分支名          本地创建和远程分支对应的分支

[root@arslinux-01 studygit]# git checkout -b dev origin/dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
切换到一个新分支 'dev'
[root@arslinux-01 studygit]# git branch
* dev
master

——对于 git push 分支分两种情况:

1)当本地分支和远程分支一致时

git push 会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用 git push origin 分支名

2)当本地分支比远程分支多

默认 git push 只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用 git push origin 分支名  如果推送失败,先用 git pull 抓取远程的新提交

一致:

[root@arslinux-01 studygit]# echo "ssssssss">2.txt
[root@arslinux-01 studygit]# echo "bbbbbbbb">>2.txt
[root@arslinux-01 studygit]# cat 2.txt
ssssssss
bbbbbbbb
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[dev e7340f2] add 2.txt
1 file changed, 2 insertions(+), 5 deletions(-)
[root@arslinux-01 studygit]# git push origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxxx/studygit.git
38e0890..e7340f2  dev -> dev

不一致:

[root@arslinux-01 studygit]# git branch dev2
[root@arslinux-01 studygit]# git checkout dev2
切换到分支 'dev2'
[root@arslinux-01 studygit]# echo "aaaaaaaaa" > 3.txt
[root@arslinux-01 studygit]# git add 3.txt
[root@arslinux-01 studygit]# git commit -m "add 3.txt"
[dev2 bdf6b7d] add 3.txt
1 file changed, 1 insertion(+)
create mode 100644 3.txt
[root@arslinux-01 studygit]# git push origin dev2
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xxxxxxx4xxxx/studygit.git
e7340f2..bdf6b7d  dev2 -> dev2


22.11 标签管理

——标签类似于快照功能,可以给版本库打一个标签,记录某个时刻库的状态,也可以随时恢复到该状态

  • git tag v1.0          给master打一个标签v1.0

  • git tag                  可以查看所有的标签

  • git show v1.0      查看标签信息

[root@arslinux-01 studygit]# git checkout master
切换到分支 'master'
[root@arslinux-01 studygit]# git tag v1.0
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git show v1.0
commit 38e08903596878b892452d53aa96dda7b76a7c64
Author: arslinux <zyxxxxxxxx@qq.com>
Date:   Thu Jul 25 22:37:14 2019 +0800
change2 2.txt
diff --git a/2.txt b/2.txt
index 77d3d2a..07ced64 100644
--- a/2.txt
+++ b/2.txt
@@ -1,4 +1,5 @@
73737372hhdjdjd
thank you
go go go
+dlj;adkjf;adfas
ore wa neko ga suki

——tag 是针对 commit 来打标签的,所以可以针对历史的 commit 来打 tag

  • git log --pretty=oneline                                  查看历史 commit

  • git log --pretty=oneline --abbrev-commit     查看历史 commit,用简写 commit

  • git tag v版本号 commit值                                 针对历史 commit 打标签

  • git tag -a v版本号 -m "描述" commit值             对标签进行描述

  • git tag -d v版本号                                              删除标签

  • git push origin v版本号                                      推送指定标签到远程

  • git push --tag v版本号                                        推送所有标签到远程

  • git tag v版本号 -d                                               删除本地标签                                               

  • git push origin:refs/tags/v版本号                    删除远程标签

[root@arslinux-01 studygit]# git log --pretty=oneline
38e08903596878b892452d53aa96dda7b76a7c64 change2 2.txt
cce24941a0e74d9c94cd05f1a59e6993ac54ca01 Update 2.txt
b2c0aa1d1b2749eeb0958ca772c378790651dd0f change README.md
6e4549cbe9f7d415eb219018b3fbbfc2e1e42c98 add 2.txt
f6afa16c437cf2917d220aace0e01963575b7383 first commit
[root@arslinux-01 studygit]# git log --pretty=oneline --abbrev-commit
38e0890 change2 2.txt
cce2494 Update 2.txt
b2c0aa1 change README.md
6e4549c add 2.txt
f6afa16 first commit
[root@arslinux-01 studygit]# git tag v0.8 6e4549cbe9f7d
[root@arslinux-01 studygit]# git tag
v0.8
v1.0
[root@arslinux-01 studygit]# git tag -a v0.1 -m "first tag" f6afa16c4
[root@arslinux-01 studygit]# git show v0.1
tag v0.1
Tagger: arslinux <zxxxxxx@qq.com>
Date:   Sat Jul 27 22:18:25 2019 +0800

first tag

commit f6afa16c437cf2917d220aace0e01963575b7383
Author: arslinux <zxxxxxxxxx@qq.com>
Date:   Thu Jul 25 22:07:51 2019 +0800

first commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..eb8db93
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# studygit
[root@arslinux-01 studygit]# git tag -d v0.1
已删除 tag 'v0.1'(曾为 7db3981)
[root@arslinux-01 studygit]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
* [new tag]         v1.0 -> v1.0
[root@arslinux-01 studygit]# git push --tag origin
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
* [new tag]         v0.8 -> v0.8
[root@arslinux-01 studygit]# git tag  v0.8 -d
已删除 tag 'v0.8'(曾为 6e4549c)
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git push origin :refs/tags/v0.8
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
To https://github.com/arsenal4life/studygit.git
- [deleted]         v0.8


22.12 git别名

  • git config --global alias.别名 命令              设置别名

  • git config --list                                          查看所有配置

  • git config --list |grep alias                          查看别名

  • git config --global --unset alias.别名          取消别名

[root@arslinux-01 studygit]# git config --global alias.ci commit
[root@arslinux-01 studygit]# echo "dafafa" >4.txt
[root@arslinux-01 studygit]# git add 4.txt
[root@arslinux-01 studygit]# git ci -m "add 4.txt"
[master 993ce4b] add 4.txt
1 file changed, 1 insertion(+)
create mode 100644 4.txt
[root@arslinux-01 studygit]# git config --global alias.br branch
[root@arslinux-01 studygit]# git br
dev
dev2
* master
[root@arslinux-01 studygit]# git config --global alias.co checkout
[root@arslinux-01 studygit]# git co dev
切换到分支 'dev'
[root@arslinux-01 studygit]# git config --list
user.email=zxxxxxxxx@qq.com
user.name=arslinux
push.default=simple
alias.ci=commit
alias.br=branch
alias.co=checkout
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/axxxxxxxxxxx/studygit.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.dev.remote=origin
branch.dev.merge=refs/heads/dev
[root@arslinux-01 studygit]# git config --list|grep alias
alias.ci=commit
alias.br=branch
alias.co=checkout
[root@arslinux-01 studygit]# git config --global --unset alias.br
[root@arslinux-01 studygit]# git br
git:'br' 不是一个 git 命令。参见 'git --help'。
您指的是这其中的某一个么?
branch
var

——git config 的配置可以在 /root/.gitconfig 中定义

查询 log 小技巧:(颜色区分显示)

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

【0718】 代码管理平台_svn_09


22.13 搭建git服务器

——github 毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。Gitlab 是个不错的选择。

1、安装 git,arslinux-02 做为服务端


[root@arslinux-02 ~]# yum install -y git

2、添加 git 用户,并设置 shell 为 /usr/bin/git-shell,目的是为了不让 git 用户远程登陆


[root@arslinux-02 ~]# useradd -s /usr/bin/git-shell git

3、创建 authorized_keys 文件,并更改属主、属组和权限,用来存客户端机器上的公钥

[root@arslinux-02 ~]# cd /home/git/
[root@arslinux-02 git]# mkdir .ssh
[root@arslinux-02 git]# touch .ssh/authorized_keys
[root@arslinux-02 git]# chmod 600 .ssh/authorized_keys
[root@arslinux-02 git]# chown -R git:git .ssh

4、将公钥添加到服务端 authorized_keys 文件中,并在客户端尝试连接

[root@arslinux-02 git]# vim .ssh/authorized_keys
[root@arslinux-01 ~]# ssh git@192.168.194.132
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.194.132 closed.

5、服务端创建 git 仓库,创建裸仓库

[root@arslinux-02 git]# cd /data/
[root@arslinux-02 data]# mkdir /data/gitroot
[root@arslinux-02 data]# cd /data/gitroot/
[root@arslinux-02 gitroot]# git init --bare sample.git
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[root@arslinux-02 gitroot]# ls
sample.git
[root@arslinux-02 gitroot]# chown -R git:git sample.git/

——git init --bare sample.git // 会创建一个裸仓库,裸仓库没有工作区,因为服务器上的 Git 仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾

——以上操作是在 git 服务器上做的,平时 git 服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像 github 一样,平时操作都是在我们自己的pc上做的

6、客户端上克隆远程仓库

[root@arslinux-01 ~]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一个空版本库。
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ll -a
总用量 4
drwxrwxr-x   3 root root   18 7月  28 12:19 .
dr-xr-x---. 10 root root 4096 7月  28 12:19 ..
drwxrwxr-x   7 root root  119 7月  28 12:19 .git

7、创建新文件到仓库,推送到远程

[root@arslinux-01 sample]# cp /etc/init.d/mysqld .
[root@arslinux-01 sample]# git add mysqld
[root@arslinux-01 sample]# git commit -m "add mysqld"
[master(根提交) 7f37e19] add mysqld
1 file changed, 378 insertions(+)
create mode 100755 mysqld
[root@arslinux-01 sample]# git push
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.84 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
* [new branch]      master -> master

如果 git push 提示没有分支,可以使用 git push origin master 来操作,会在裸仓库创建新分支

8、再推送新文件就不会提示了

[root@arslinux-01 sample]# echo "dafasdfasdf" >222.txt
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "add 222.txt"
[master a9de871] add 222.txt
1 file changed, 1 insertion(+)
create mode 100644 222.txt
[root@arslinux-01 sample]# git push
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)
To git@192.168.194.132:/data/gitroot/sample.git
7f37e19..a9de871  master -> master

9、不妨到 /tmp/ 目录下,克隆服务端的 sample.git,可以看到,客户端新建文件已经推到服务端了

[root@arslinux-01 sample]# cd /tmp/
[root@arslinux-01 tmp]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), 4.09 KiB | 0 bytes/s, done.
[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld

10、如果有多个服务器连接 git 服务端,而且其他服务器对服务端做了更改,那么客户端可以 git pull 来更新仓库(/tmp/sample/ 和 /data/sample/ 就当做两个服务器)

[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# echo "435678" >> 222.txt
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "ch 222.txt"
[master 4e5ad08] ch 222.txt
1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# git push
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
a9de871..4e5ad08  master -> master
[root@arslinux-01 sample]# cd
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 192.168.194.132:/data/gitroot/sample
a9de871..4e5ad08  master     -> origin/master
更新 a9de871..4e5ad08
Fast-forward
222.txt | 1 +
1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# cat 222.txt
dafasdfasdf
435678

这样就可以非常方便协同操作


22.14/22.15 安装gitlab

gitlab 官网 https://about.gitlab.com/gitlab-com/

官方安装文档:https://about.gitlab.com/install/#centos-7

要求服务器内存不少于2g,否则会卡死

1、创建 gitlab 的安装源

[root@arslinux-01 ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

2、安装 gitlab


[root@arslinux-01 ~]# yum install -y gitlab-ce

3、自动配置 gitlab

[root@arslinux-01 ~]# gitlab-ctl reconfigure
[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.194.130:27017   0.0.0.0:*               LISTEN      7504/mongod
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      7504/mongod
tcp        0      0 127.0.0.1:9100          0.0.0.0:*               LISTEN      14204/node_exporter
tcp        0      0 127.0.0.1:9229          0.0.0.0:*               LISTEN      14189/gitlab-workho
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 127.0.0.1:9168          0.0.0.0:*               LISTEN      14222/puma 3.12.0 (
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      12872/unicorn maste
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12951/nginx: master
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7332/rpc.mountd
tcp        0      0 0.0.0.0:37361           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:8082          0.0.0.0:*               LISTEN      12888/sidekiq 5.2.7
tcp        0      0 127.0.0.1:9236          0.0.0.0:*               LISTEN      14209/gitaly
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7242/sshd
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      15042/grafana-serve
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7675/master
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      12951/nginx: master
tcp        0      0 0.0.0.0:35038           0.0.0.0:*               LISTEN      7265/rpc.statd
tcp        0      0 127.0.0.1:9121          0.0.0.0:*               LISTEN      14233/redis_exporte
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      14240/prometheus
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      7256/zabbix_agentd
tcp        0      0 127.0.0.1:9187          0.0.0.0:*               LISTEN      14284/postgres_expo
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      7330/zabbix_server
tcp        0      0 127.0.0.1:9093          0.0.0.0:*               LISTEN      14275/alertmanager
tcp6       0      0 :::3306                 :::*                    LISTEN      7579/mysqld
tcp6       0      0 :::33836                :::*                    LISTEN      -
tcp6       0      0 :::53101                :::*                    LISTEN      7265/rpc.statd
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 ::1:9168                :::*                    LISTEN      14222/puma 3.12.0 (
tcp6       0      0 :::20048                :::*                    LISTEN      7332/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7242/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7675/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::10050                :::*                    LISTEN      7256/zabbix_agentd
tcp6       0      0 :::10051                :::*                    LISTEN      7330/zabbix_server
tcp6       0      0 :::10052                :::*                    LISTEN      7264/java
tcp6       0      0 :::9094                 :::*                    LISTEN      14275/alertmanager

4、关闭 gitlab,可以看到 gitlab 相关服务

[root@arslinux-01 ~]# 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: grafana: 0s, normally up
ok: down: logrotate: 1s, normally up
ok: down: nginx: 0s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 1s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 1s, normally up

5、停止本地之前安装的 nginx 服务,redis-server

[root@arslinux-01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl):                            [  确定  ]
[root@arslinux-01 ~]# chkconfig nginx off
[root@arslinux-01 ~]# killall redis-server

6、启动 gitlab(gitlab-ctl stop/restart/start/status)

[root@arslinux-01 ~]# gitlab-ctl start
ok: run: alertmanager: (pid 15894) 1s
ok: run: gitaly: (pid 15905) 0s
ok: run: gitlab-monitor: (pid 15923) 1s
ok: run: gitlab-workhorse: (pid 15925) 0s
ok: run: grafana: (pid 15932) 1s
ok: run: logrotate: (pid 15938) 0s
ok: run: nginx: (pid 15946) 1s
ok: run: node-exporter: (pid 15951) 0s
ok: run: postgres-exporter: (pid 15955) 1s
ok: run: postgresql: (pid 15960) 0s
ok: run: prometheus: (pid 15962) 1s
ok: run: redis: (pid 15976) 0s
ok: run: redis-exporter: (pid 15981) 1s
ok: run: sidekiq: (pid 15986) 0s
ok: run: unicorn: (pid 15992) 0s

7、网页访问 gitlab,只要输入 ip 地址即可

如果出现 502 错误,那么请检查内存是否过小,一般不小于 4G

【0718】 代码管理平台_gitlab_10

8、修改密码后,登录,默认用户名 root,密码就是刚刚修改的

登录后就可以创建项目

【0718】 代码管理平台_GitHub_11

22.16 使用gitlab

gitlab常用命令:https://www.cnyunwei.cc/archives/1204

进入 gitlab,先创建组,再创建项目

1、创建组:

【0718】 代码管理平台_git_12

2、创建项目:

【0718】 代码管理平台_GitHub_13

和 github 一样,需要添加 ssh key,才可以和 gitlab 通信

【0718】 代码管理平台_git_14

3、设置 ssh key

右上角头像——setting——SSH Keys

【0718】 代码管理平台_git_15

4、创建用户

顶部中间扳手图标(Admin Area)——New user

【0718】 代码管理平台_svn_16

【0718】 代码管理平台_svn_17

5、进入用户编辑,可以设定用户密码

【0718】 代码管理平台_svn_18【0718】 代码管理平台_分支_19

6、退出,用新建的用户名密码登录,首次登录需要修改密码,可以设成和原来一样的密码,然后就可以创建项目了

【0718】 代码管理平台_GitHub_20


22.17 gitlab备份和恢复

——gitlab 备份


[root@arslinux-01 ~]# gitlab-rake gitlab:backup:create

——备份的文件默认会放到 /var/opt/gitlab/backups/ 下

[root@arslinux-01 ~]# ls /var/opt/gitlab/backups/
1564299157_2019_07_28_12.1.1_gitlab_backup.tar
[root@arslinux-01 ~]# du -sh /var/opt/gitlab/backups/1564299157_2019_07_28_12.1.1_gitlab_backup.tar
132K/var/opt/gitlab/backups/1564299157_2019_07_28_12.1.1_gitlab_backup.tar

——gitlab 恢复

1)暂停 unicorn、sidekiq 服务

[root@arslinux-01 ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq

2)恢复 gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀)

[root@arslinux-01 ~]# gitlab-rake gitlab:backup:restore BACKUP=1564299157_2019_07_28_12.1.1

——再启动服务 gitlab-ctl start