[toc]

前言

不管是做为程序猿还是运维工程师,很多人一定都听说过GitHub 。

分布式版本控制系统

版本控制

不管实在企业中,在个人,都接触过版本控制
比如:

  • 脚本 一遍一遍修改
  • 论文 一遍一遍修改

什么是分布式

分布式系统是由一组通过网络进行通信、为了完成共同的任务而协调工作的计算机节点组成的系统。分
布式系统的出现是为了用廉价的、普通的2机器完成单个计算机无法完成的计算、存储任务。其目的是利
用更多的机器,处理更多的数据。

因为 Git 是分布式的,所以 Git 支持离线工作,在本地可以进行很多操作,包括接下来将要重磅推出的
分支功能.

git本地仓库

git部署

1
2
3
4
5
# 安装git
yum install -y git

# 查看版本号
git --version

web操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装nginx
yum install -y nginx

# 编辑配置文件
server{
listen 80;
server_name _;
root /code/web;
index index.html;
}

# 创建站点目录
mkdir /code/web

# 启动服务
systemctl start nginx

使用git管理

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 创建目录
mkdir web

# 进入目录
cd web

# 将当前的目录,初始化成git仓库
git init

# 添加git优化配置颜色
git config --global color.ui auto

# 查看.git
ll -a
total 4
drwxr-xr-x 3 root root 18 May 11 14:47 .
dr-xr-x---. 10 root root 4096 May 11 14:45 ..
drwxr-xr-x 7 root root 119 May 11 14:47 .git

# 查看当前git仓库的配置文件
cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

# 查看隐藏文件目录结构
tree -a
.
└── .git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags

10 directories, 13 files

gitlib本地仓库版本控制并实现需求

需求一

老板:给我写一个官网
程序猿:一天一夜,写出来了,请CEO过目

在仓库中写代码

  • html代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # cat index.html
    <!DOCTYPE html>
    <html lang="zh">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码迭代过程-曾老湿</title>
    </head>
    <body>
    <div id="demo"></div>
    <script src="src.js"></script>
    </body>
    </html>
  • JS代码

    1
    2
    3
    4
    5
    6
    7
    8
    # cat src.js
    const string = '老板好,我是程序猿:zls,您让我写的官网页面,它会动'
    let n = 1
    demo.innerHTML = string.substring(0,n)
    setInterval(()=>{
    n+=1
    demo.innerHTML = string.substring(0,n)
    },200)

使用git进行版本控制

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
# 查看git仓库状态
git status
# On branch master
#
# Initial commit
#
# Untracked files: 这两个文件还没有被添加进去
# (use "git add <file>..." to include in what will be committed)
#
# index.html
# src.js 可以用git add去添加
nothing added to commit but untracked files present (use "git add" to track)


# 将当前目录下所有文件添加到暂存区
git add .

# 查看git仓库状态
git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
# new file: src.js
#

image-20230514202844825

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 添加git用户设置
git config --global user.email "1944119015@qq.com"
git config --global user.name "xxx"

# 提交代码到git仓库
git commit -m '源代码v1.0'

# 查看提交信息
git log
commit 71ba27192e888af354cc2ca33ddd3fb340364b00
Author: Administrator <admin@example.com>
Date: Thu May 11 14:55:22 2023 +0800
源代码v1.0

# 代码上线
scp /aaa/* 10.0.0.7:/code/web

image-20230514201333825

需求二

老板:不够醒目,再改改
程序猿:好嘞,花了一周时间,请CEO过目

在仓库中写代码

  • html代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # cat index.html
    <!DOCTYPE html>
    <html lang="zh">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
    #demo{
    border: solid 1px red;
    width: 410px;
    height: 25px;
    background-color: lightpink;
    }
    </style>
    <title>代码迭代过程-曾老湿111</title>
    </head>
    <body>
    <div id="demo"></div>
    <script src="src.js"></script>
    </body>
    </html>
  • JS代码

    1
    2
    3
    4
    5
    6
    7
    8
    # cat src.js
    const string = '老板好,我是程序猿:zls,您让我写的官网页面,它会动'
    let n = 1
    demo.innerHTML = string.substring(0,n)
    setInterval(()=>{
    n+=1
    demo.innerHTML = string.substring(0,n)
    },200)

使用git进行版本控制

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
# 查看git状态
git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

# 将index文件添加到暂存区
git add index.html

# 将修改的文件重新提交到git仓库中
git commit -m '源代码

# 查看日志
git log
commit b892777227242a583ff03a09ab09308777b23588
Author: Administrator <admin@example.com>
Date: Thu May 11 15:05:57 2023 +0800

源代码v1.1(醒目)

commit 71ba27192e888af354cc2ca33ddd3fb340364b00
Author: Administrator <admin@example.com>
Date: Thu May 11 14:55:22 2023 +0800

#代码上线
scp ./* 10.0.0.7:/code/web

image-20230514201806000

需求三

老板:还是之前的好看,改回去吧。
程序猿:emmmmmm… 老子不干了。妈的,我该怎么撤回一周内容?

代码回滚

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
35
36
37
38
39
40
41
# 查看日志
git log
commit b892777227242a583ff03a09ab09308777b23588
Author: Administrator <admin@example.com>
Date: Thu May 11 15:05:57 2023 +0800

源代码v1.1(醒目)

commit 71ba27192e888af354cc2ca33ddd3fb340364b00
Author: Administrator <admin@example.com>
Date: Thu May 11 14:55:22 2023 +0800

源代码v1.0

# 查看完整的日志
git reflog
b892777 HEAD@{0}: reset: moving to b892
71ba271 HEAD@{1}: reset: moving to 71ba
b892777 HEAD@{2}: commit: 源代码v1.1(醒目)
71ba271 HEAD@{3}: commit (initial): 源代码v1.0

# 回滚到指定版本
git reset --hard 71ba
HEAD is now at 71ba271 源代码v1.0

# 查看index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>代码迭代过程-曾老湿</title>
</head>
<body>
<div id="demo"></div>
<script src="src.js"></script>
</body>
</html>

# 代码上线
scp ./* 10.0.0.7:/code/web

image-20230514204838454