似水流年,转眼到了不惑之年。我和大家一样,对周围的事逐渐司空见惯。过去的事过去了,未过去的事也不能叫我惊讶。——–王小波
写在前面
嗯,Git
一般都用平台上的,自己用Githup
,Gitee
,公司用的Gitlab
,服务端的没搞过,基本用的都是客户端。
之前实习分了一个项目做组长,所以自己在阿里云上面搭了SVN
的服务端,折腾了大半夜,但是后来也没怎么用。感兴趣小伙伴可以去围观:阿里云Linux下搭建SVN服务端
想把Linux
的东西总结一下,然后告一段落,学学微服务,python,考一个华为RS认证。
笔记基本是听课整理的笔记,不足之处小伙伴留言。另准备自己在服务器上搭一个Gitlab.
似水流年,转眼到了不惑之年。我和大家一样,对周围的事逐渐司空见惯。过去的事过去了,未过去的事也不能叫我惊讶。——–王小波
一、Git概述 版本控制 版本控制系统就是一个控制文件版本的系统
版本仓库
是版本控制的核心
任意客户端可以连接使用版本仓库
主流的版本控制系统
集中式
版本控制系统
所有客户端共享一个仓库
所有操作都需要联网
代表软件:Subversion
分布式
版本控制系统
每个客户端都有一个仓库
的完整克隆
支持断网操作
代表软件:Git
集中式版本控制SVC
服务器记录所有版本,客户端仅有最新版本,无法实现断网操作! 集中式版本控制
客户端只会保留最新
得版本号
;
如果发生断网
,客户端
继续版本更新,就会导致丢失断网期间的数据版本
分布式版本控制
服务器记录所有版本,客户端也有所有版本,具有本地仓库功能,支持断网操作
! 分布式版本控制
客户端拥有本地仓库,会保留所以的历史版本;
如果发生断网,客户端继续版本更新,数据修改的版本会被提交到本地仓库;
当网络回复时,客户端会将所有本地仓库的版本数据提交到远程服务器上
Git基本概念
Git 仓库
: 保存所有数据的地方
工作区
: 实时编辑文件的地方 (只看到一个文件)
暂存区
: 就是一个文件,索引文件,保存下次将提交的文件列表信息
简易的命令行入门教程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 git config --global user.name "意必固我" git config --global user.email "1224965096@qq.com" mkdir Demos cd Demosgit init touch README.md git add README.md git commit -m "first commit" git remote add origin git@gitee.com:liruilonger/Demos.git git push -u origin master cd existing_git_repogit remote add origin git@gitee.com:liruilonger/Demos.git git push -u origin master
Git工作流
部署Git服务 部署Git仓库
安装软件(192.168.2.100作为远程Git服务器)
1 2 3 4 5 6 7 8 9 10 11 [root@web1 ~]$ mkdir /var/git [root@web1 ~]$ git init /var/git/project --bare 初始化空的 Git 版本库于 /var/git/project/ [root@web1 ~]$ ls /var/git/project/ branches description hooks objects config HEAD info refs git
客户端测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@liruilongs.github.io ~]$ yum -y install git [root@liruilongs.github.io ~]$ git clone root@192.168.2.100:/var/git/project 正克隆到 'project' ... The authenticity of host '192.168.2.100 (192.168.2.100)' can't be established. ECDSA key fingerprint is SHA256:wpTzE5md4arrZVRHhaKFacrEHYWldSWAPwghApqXXfo. ECDSA key fingerprint is MD5:7d:d3:26:9d:05:b4:f5:e0:0e:e4:2d:34:2d:c5:a4:3e. Are you sure you want to continue connecting (yes/no)? yes ##ssh连接 Warning: Permanently added ' 192.168.2.100' (ECDSA) to the list of known hosts. root@192.168.2.100' s password: warning: 您似乎克隆了一个空版本库。 [root@liruilongs.github.io ~]$ ls /root/project/ [root@liruilongs.github.io ~]$ ls -a /root/project/ . .. .git
二、GIt版本控制 提交数据 在客户端对仓库进行编辑,创建新资料:注意:liruilongs.github.io为客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@liruilongs.github.io ~]$ cd /root/project/ [root@liruilongs.github.io project]$ mkdir demo [root@liruilongs.github.io project]$ cp /etc/hosts demo/ [root@liruilongs.github.io project]$ cp /etc/passwd init.txt [root@liruilongs.github.io project]$ git status 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
提交数据到本地版本仓库
提交暂存区
1 2 3 4 5 6 [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ ls -a . .. demo .git init.txt
将暂存区修改提交到本地仓库
1 2 3 4 5 6 7 8 9 [root@liruilongs.github.io project]$ git config --global user.email "zhang@163.com" [root@liruilongs.github.io project]$ git config --global user.name "zhang san" [root@liruilongs.github.io project]$ git commit -m "first file version" [master(根提交) 6023adb] first file version 2 files changed, 23 insertions(+) create mode 100644 demo/init.txt create mode 100644 init.txt
将数据推送至远程Git服务器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@liruilongs.github.io project]$ git config --global push.default simple [root@liruilongs.github.io project]$ git push root@192.168.2.100 s password: Counting objects: 5, done . Compressing objects: 100% (4/4), done . Writing objects: 100% (5/5), 787 bytes | 0 bytes/s, done . Total 5 (delta 0), reused 0 (delta 0) To root@192.168.2.100:/var/git/project * [new branch] master -> master [root@liruilongs.github.io project]$ git pull root@192.168.2.100's password: #输入密码 Already up-to-date.
生成更多版本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 [root@liruilongs.github.io project]$ echo "new file" > new.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "add new.txt" [master 02d4ef9] add new.txt 1 file changed, 1 insertion(+) create mode 100644 new.txt [root@liruilongs.github.io project]$ echo "first" >> new.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "new.txt:first line" [master 7dc8c9e] new.txt:first line 1 file changed, 1 insertion(+) [root@liruilongs.github.io project]$ echo "second" >> new.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "new.txt:second" [master c191194] new.txt:second 1 file changed, 1 insertion(+) [root@liruilongs.github.io project]$ echo "third" >> new.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "new.txt:thired" [master ebfe460] new.txt:thired 1 file changed, 1 insertion(+) [root@liruilongs.github.io project]$ git push root@192.168.2.100's password: Counting objects: 13, done. Compressing objects: 100% (8/8), done. Writing objects: 100% (12/12), 961 bytes | 0 bytes/s, done. Total 12 (delta 3), reused 0 (delta 0) To root@192.168.2.100:/var/git/project 6023adb..ebfe460 master -> master
多次修改数据,生成更多版本信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@liruilongs.github.io project]$ echo "123" > num.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "num.txt:123" [master b35b1ce] num.txt:123 1 file changed, 1 insertion(+) create mode 100644 num.txt [root@liruilongs.github.io project]$ echo "456" > num.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "num.txt:456" [master 9595def] num.txt:456 1 file changed, 1 insertion(+), 1 deletion(-) [root@liruilongs.github.io project]$ echo "789" > num.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "num:txt:789" [master 30d20ed] num:txt:789 1 file changed, 1 insertion(+), 1 deletion(-) [root@liruilongs.github.io project]$ git push root@192.168.2.100's password: Counting objects: 10, done. Compressing objects: 100% (6/6), done. Writing objects: 100% (9/9), 663 bytes | 0 bytes/s, done. Total 9 (delta 3), reused 0 (delta 0) To root@192.168.2.100:/var/git/project ebfe460..30d20ed master -> master
查看日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [root@liruilongs.github.io project]$ git log ...... commit 6023adb8f6c785c41b1857e5027d34904b25ed05 Author: zhang san <zhang@163.com> Date: Thu Jul 30 10:36:19 2020 +0800 first file version ...... [root@liruilongs.github.io project]$ git log --pretty=oneline ...... 7dc8c9e0e86c0eb3a5f976380e90f100cb6da999 new.txt:first line 02d4ef9d70844268d8c6048b1133dca2c1a5bc27 add new.txt 6023adb8f6c785c41b1857e5027d34904b25ed05 first file version ...... [root@liruilongs.github.io project]$ git log --oneline ...... 7dc8c9e new.txt:first line 02d4ef9 add new.txt 6023adb first file version ...... [root@liruilongs.github.io project]$ git reflog ...... 7dc8c9e HEAD@{5}: commit: new.txt:first line 02d4ef9 HEAD@{6}: commit: add new.txt 6023adb HEAD@{7}: commit (initial): first file version .....
数据恢复 HEAD指针下
HEAD指针
是一个可以在任何分支
和任何版本
移动的指针
,通过移动过指针
我们可以将数据还原至任何版本
,当前HEAD指针
为HEAD@{0}
我们可以使用 git relog 查看当前指针
1 2 3 4 5 6 7 8 9 10 [root@liruilongs.github.io project]$ git reflog 30d20ed HEAD@{0}: commit: num:txt:789 9595def HEAD@{1}: commit: num.txt:456 b35b1ce HEAD@{2}: commit: num.txt:123 ebfe460 HEAD@{3}: commit: new.txt:thired c191194 HEAD@{4}: commit: new.txt:second 7dc8c9e HEAD@{5}: commit: new.txt:first line 02d4ef9 HEAD@{6}: commit: add new.txt 6023adb HEAD@{7}: commit (initial): first file version HEAD
查看Git日志信息 1 2 3 4 5 6 7 8 9 10 [root@liruilongs.github.io project]$ git log --oneline 30d20ed num:txt:789 9595def num.txt:456 b35b1ce num.txt:123 ebfe460 new.txt:thired c191194 new.txt:second 7dc8c9e new.txt:first line 02d4ef9 add new.txt 6023adb first file version
移动HEAD指計 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@liruilongs.github.io project]$ git reset --hard b35b1ce HEAD 现在位于 b35b1ce num.txt:123 [root@liruilongs.github.io project]$ git reflog | head -2 b35b1ce HEAD@{0}: reset: moving to b35b1ce 30d20ed HEAD@{1}: commit: num:txt:789 [root@liruilongs.github.io project]$ cat num.txt 123 [root@liruilongs.github.io project]$ git reset --hard 02d4ef9 HEAD 现在位于 02d4ef9 add new.txt [root@liruilongs.github.io project]$ git reflog | head -2 02d4ef9 HEAD@{0}: reset: moving to 02d4ef9 b35b1ce HEAD@{1}: reset: moving to b35b1ce [root@liruilongs.github.io project]$ ls demo init.txt new.txt [root@liruilongs.github.io project]$ cat new.txt new file [root@liruilongs.github.io project]$ git reset --hard 30d20ed HEAD 现在位于 30d20ed num:txt:789
分支
分支可以让开发分多条主线同时进行,每个主线互不影响
常见分支规范如下:/常见的分支规范如下:
默认master分支MASTER是主分支
,是代码的核心
DEVELOP最新开发成果的分支
RELEASE分支
,为发布新产品设置的分支
HOTFIX分支
,为了修复软件BUG缺陷的分支
FEATURE分支
,为开发新功能设置的分支
1 2 3 4 5 MASTER分支 DEVELOP分支 RELEASE分支 HOTFIX分支 FEATURE分支
概述暂存区修改提交到本地仓库 创建分支
查看当前分支
1 2 3 4 5 6 7 [root@liruilongs.github.io project]$ git status 无文件要提交,干净的工作区 [root@liruilongs.github.io project]$ git branch -v * master 30d20ed num:txt:789
创建分支
1 2 3 4 5 6 7 8 [root@liruilongs.github.io project]$ git branch hotfix [root@liruilongs.github.io project]$ git branch feature [root@liruilongs.github.io project]$ git branch -v feature 30d20ed num:txt:789 hotfix 30d20ed num:txt:789 * master 30d20ed num:txt:789
切换与合并分支
切换分支
1 2 3 4 5 6 7 8 [root@liruilongs.github.io project]$ git checkout hotfix 切换到分支 'hotfix' [root@liruilongs.github.io project]$ git branch -v feature 30d20ed num:txt:789 * hotfix 30d20ed num:txt:789 master 30d20ed num:txt:789
在新分支上进行数据操作(增、删、改、查)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@liruilongs.github.io project]$ echo "fix a bug" >> new.txt [root@liruilongs.github.io project]$ cat new.txt new file first second third fix a bug [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "fix a bug" [hotfix e686c17] fix a bug 1 file changed, 1 insertion(+)
合并分支:将 hotfix 修改的数据合并到 master 分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@liruilongs.github.io project]$ git checkout master 切换到分支 'master' [root@liruilongs.github.io project]$ cat new.txt new file first second third [root@liruilongs.github.io project]$ git branch -v feature 30d20ed num:txt:789 hotfix e686c17 fix a bug * master 30d20ed num:txt:789 [root@liruilongs.github.io project]$ git merge hotfix 更新 30d20ed..e686c17 Fast-forward new.txt | 1 + 1 file changed, 1 insertion(+) [root@liruilongs.github.io project]$ cat new.txt new file first second third fix a bug
解决分支的版本冲实问题
在不同分支中修改相同文件的相同行数据,模拟数据冲突
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [root@liruilongs.github.io project]$ git checkout hotfix 切换到分支 'hotfix' [root@liruilongs.github.io project]$ echo AAA > a.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "add a.txt by hotfix" [hotfix d96f8ac] add a.txt by hotfix 1 file changed, 1 insertion(+) create mode 100644 a.txt [root@liruilongs.github.io project]$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交) [root@liruilongs.github.io project]$ echo "BBB" > a.txt [root@liruilongs.github.io project]$ git add . [root@liruilongs.github.io project]$ git commit -m "add a.txt by master" [master afd5c52] add a.txt by master 1 file changed, 1 insertion(+) create mode 100644 a.txt [root@liruilongs.github.io project]$ git merge hotfix 自动合并 a.txt 冲突(添加/添加):合并冲突于 a.txt 自动合并失败,修正冲突然后提交修正的结果。
查看有冲突的文件,修改文件为最终版本的数据,解决冲突
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@liruilongs.github.io project]$ cat a.txt <<<<<<< HEAD BBB ======= AAA >>>>>>> hotfix ###冲突需要自己手动解决,修改master分支中的数据,则为最终数据 [root@liruilongs.github.io project]$ vim a.txt #留下的为最终数据 BBB ###将修改提交到暂存区 [root@liruilongs.github.io project]$ git add . ###将暂存区中的数据提交到本地,备注冲突解决 [root@liruilongs.github.io project]$ git commit -m "resolved" [master b5864d8] resolved
增量升级时,导出所有更新文件
1 2 3 4 5 6 7 8 D:\iwhalecloud\uncp\uncp-all>git log --pretty=oneline -2 b3406f5801f04eef605da8fa54259efedf105fc5 (HEAD -> neimeng) <E5><86><85><E8><92><99><E4><BB><A3><E7><A0><81><E6><94><B9><E9><80><A0> ada4c1075af4f98b3a38491cd6aa73ef48098a68 (origin/neimeng, origin/master, origin/guangxi, origin/HEAD, master) Merge branch 'master' of http://gitlab.iwhalecloud.com/uncp-all/uncp-all D:\iwhalecloud\uncp\uncp-all> PS D:\iwhalecloud\uncp\uncp-all> git archive -o ./export.zip b3406f5801f04eef605da8fa54259efedf105fc5 $(git diff --name-only ada4c1075af4f98b3a38491cd6aa73ef48098a68 b3406f5801f04eef605da8fa54259efedf105fc5 ) PS D:\iwhalecloud\uncp\uncp-all> git reflog
git命令
1 git archive -o ./export.zip NewCommitId $(git diff --name-only OldCommitId NewCommitId)
三、Gitlab搭建
服务器: liruilongs.github.io:192.168.26.55
一、docker 环境安装 1 2 3 4 ┌──[root@liruilongs.github.io]-[~] └─$ yum -y install docker-ce ┌──[root@liruilongs.github.io]-[~] └─$ systemctl enable docker --now
配置docker加速器
1 2 3 4 5 6 7 8 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors" : ["https://2tefyfv7.mirror.aliyuncs.com" ] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
二、安装GitLab 1.安装GitLab 并配置 拉取镜像
1 2 ┌──[root@liruilongs.github.io]-[~] └─$ docker pull beginor/gitlab-ce
–
2.创建共享卷目录 创建共享卷目录,用于持久化必要的数据和更改相关配置
1 2 3 4 ┌──[root@liruilongs.github.io]-[~] └─$ mkdir -p /data/gitlab/etc/ /data/gitlab/log /data/gitlab/data ┌──[root@liruilongs.github.io]-[~] └─$ chmod 777 /data/gitlab/etc/ /data/gitlab/log /data/gitlab/data
3.创建 Gitlab 容器 这里的访问端口一定要要设置成80,要不push项目会提示没有报错,如果宿主机端口被占用,需要把这个端口腾出来
1 2 3 4 5 6 7 8 9 10 11 ┌──[root@liruilongs.github.io]-[~] └─$ docker run -itd --name=gitlab --restart=always --privileged=true -p 8443:443 -p 80:80 -p 222:22 -v /data/gitlab/etc:/etc/gitlab -v /data/gitlab/log :/var/log /gitlab -v /data/gitlab/data:/var/opt/gitlab beginor/gitlab-ce acc95b2896e8475915275d5eb77c7e63f63c31536432b68508f2f216d4fec634 ┌──[root@liruilongs.github.io]-[~] └─$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES acc95b2896e8 beginor/gitlab-ce "/assets/wrapper" 53 seconds ago Up 51 seconds (health: starting) 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:222->22/tcp, :::222->22/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp gitlab ┌──[root@liruilongs.github.io]-[~] └─$ ┌──[root@liruilongs.github.io]-[~] └─$#
4.关闭容器修改相关配置文件 1 2 3 ┌──[root@liruilongs.github.io]-[~] └─$ docker stop gitlab gitlab
external_url ‘http://192.168.26.55'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep external_url ┌──[root@liruilongs.github.io]-[~] └─$ sed -i "/external_url 'GENERATED_EXTERNAL_URL'/a external_url\t'http://192.168.26.55' " /data/gitlab/etc/gitlab.rb ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep external_url external_url 'http://192.168.26.55' ┌──[root@liruilongs.github.io]-[~] └─$
gitlab_rails[‘gitlab_ssh_host’] = ‘192.168.26.55’
1 2 3 4 5 6 7 8 9 10 11 ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep gitlab_ssh_host ┌──[root@liruilongs.github.io]-[~] └─$ sed -i "/gitlab_ssh_host/a gitlab_rails['gitlab_ssh_host'] = '192.168.26.55' " /data/gitlab/etc/gitlab.rb ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep gitlab_ssh_host gitlab_rails['gitlab_ssh_host' ] = '192.168.26.55' ┌──[root@liruilongs.github.io]-[~] └─$
gitlab_rails[gitlab_shell_ssh_port] = 222
1 2 3 4 5 6 7 8 9 10 11 ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep gitlab_shell_ssh ┌──[root@liruilongs.github.io]-[~] └─$ sed -i "/gitlab_shell_ssh_port/a gitlab_rails['gitlab_shell_ssh_port'] = 222" /data/gitlab/etc/gitlab.rb ┌──[root@liruilongs.github.io]-[~] └─$ cat /data/gitlab/etc/gitlab.rb | grep gitlab_shell_ssh gitlab_rails[gitlab_shell_ssh_port] = 222 ┌──[root@liruilongs.github.io]-[~] └─$
/data/gitlab/data/gitlab-rails/etc/gitlab.yml
1 2 3 4 5 6 7 8 9 10 ┌──[root@liruilongs.github.io]-[~] └─$ vim /data/gitlab/data/gitlab-rails/etc/gitlab.yml ┌──[root@liruilongs.github.io]-[~] └─$ gitlab: host: 192.168.26.55 port: 80 https: false
–
修改完配置文件之后。直接启动容器
1 2 ┌──[root@liruilongs.github.io]-[~] └─$ docker start gitlab
5.访问测试
访问测试
在宿主机所在的物理机访问,http://192.168.26.55/
,会自动跳转到修改密码(root用户),如果密码设置的没有满足一定的复杂性,则会报500,需要从新设置
登录进入仪表盘
三、新建项目,push代码测试
新建一个项目,push代码测试
然后我们简单测试一下,push一个项目上去
项目成功上传Gitlab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 PS F:\blogger> git init Initialized empty Git repository in F:/blogger/.git/ PS F:\blogger> git config --global user.name "Administrator" PS F:\blogger> git config --global user.email "admin@example.com" PS F:\blogger> git remote add origin http://192.168.26.55/root/blog.git PS F:\blogger> git add . PS F:\blogger> git commit -m "Initial commit" PS F:\blogger> git push -u origin master Enumerating objects: 322, done . Counting objects: 100% (322/322), done . Delta compression using up to 8 threads Compressing objects: 100% (302/302), done . Writing objects: 100% (322/322), 11.31 MiB | 9.22 MiB/s, done . Total 322 (delta 24), reused 0 (delta 0) remote: Resolving deltas: 100% (24/24), done . To http://192.168.26.55/root/blog.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin' . PS F:\blogger>
时间原因,关于 【搭建自己的中文Git版本库】先分享到这里。生活加油 ^ _ ^