Git--02 Devops介绍及git安装部署

1. Devops介绍

01. 运维介绍

一般来说,运维工程师在一家企业里属于个位数的岗位,甚至只有一个。面对生产中NNN台服务器,NN个人员,工作量也是非常大的。

MTBF、 MTTR 、MTTF 详解

MTBF (Mean Time Between Failures) =平均故障间隔时间 MTTF

MTTF (Mean Time To Failure) =平均故障时间 

MTTR (Mean Time To Repair) =平均修复时间

3个9:(1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小时。
4个9:(1-99.99%)*365*24=0.876小时=52.6分钟,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是52.6分钟。
5个9:(1-99.999%)*365*24*60=5.26分钟,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是5.26分钟。
6个9:(1-99.9999%)*365*24*60*60=31秒

代码上线流程图

运维工程师三大核心职能

02. Devops是什么

开发 development
运维 operations

03. Devops能干嘛

提高产品质量

1 自动化测试2 持续集成3 代码质量管理工具4 程序员鼓励师

04. Devops如何实现

既然这么好?为什么有些公司没有设计架构规划-代码的存储-构建-测试、预生产、部署、监控

2. Git版本控制系统

01. 版本控制系统简介

vcs version control system

版本控制系统是一种记录一个或若干个文件内容变化,以便将来查阅特定版本内容情况的系统
记录文件的所有历史变化
随时可恢复到任何一个历史状态
多人协作开发

02. 为什么需要版本控制系统

03. 常见版本管理工具

SVN集中式的版本控制系统,只有一个中央数据仓库,如果中央数据仓库挂了或者不可访问,所有的使用者无法使用SVN,无法进行提交或备份文件。

04. 牛逼的人不需要解释

这句话被Linux展现的淋漓尽致

3. Git安装

01. 系统环境准备

[root@git ~]# cat /etc/redhat-release    
#查看系统版本
CentOS Linux release 7.6.1810 (Core)

[root@git ~]# uname -r    
#查看内核版本
3.10.0-957.el7.x86_64
[root@git ~]# getenforce    
#确认Selinux关闭状态Disabled
[root@git ~]# systemctl status firewalld     
#关闭防火墙
firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)

02. Git安装部署

[root@git ~]# yum install git -y    #安装Git
[root@git ~]# git config
--global use global config file        #使用全局配置文件
--system use system config file        #使用系统级配置文件
--local use repository config file    #使用版本库级配置文件

 #配置git使用用户
[root@git ~]# git config --global user.name "gjy"
#配置git使用邮箱
[root@git ~]# git config --global user.email "[email protected]"
#语法高亮
[root@git ~]# git config --global color.ui true
#检查结果
[root@git ~]# git config --list
user.name=gjy
[email protected]
color.ui=true

#查看隐藏文件
[root@git ~]# ll -a
total 40
dr-xr-x---.  2 root root  187 Nov 26  2019 .
dr-xr-xr-x. 17 root root  224 Aug  2 04:55 ..
-rw-------.  1 root root 1430 Aug  2 04:57 anaconda-ks.cfg
-rw-------.  1 root root 2561 Nov 25  2019 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
-rw-r--r--   1 root root   64 Nov 26  2019 .gitconfig
-rwxr-xr-x.  1 root root  473 Aug  1 20:44 host_ip.sh
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
-rw-------   1 root root 1394 Nov 26  2019 .viminfo

[root@git ~]# cat .gitconfig 
[user]
    name = gjy
    email = [email protected]
[color]
    ui = true

03. Git初始化

#初始化工作目录、对已存在的目录或者对已存在的目录都可进行初始化
#创建一个仓库目录
[root@git ~]# mkdir git_data
[root@git ~]# cd git_data/
#初始化
[root@git ~/git_data]# git init
Initialized empty Git repository in /root/git_data/.git/

#查看工作区状态
[root@git git_data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

[root@git git_data]# ll .git/
total 12
drwxr-xr-x 2 root root   6 Nov 25 16:50 branches
-rw-r--r-- 1 root root  92 Nov 25 16:50 config
-rw-r--r-- 1 root root  73 Nov 25 16:50 description
-rw-r--r-- 1 root root  23 Nov 25 16:50 HEAD
drwxr-xr-x 2 root root 242 Nov 25 16:50 hooks
drwxr-xr-x 2 root root  21 Nov 25 16:50 info
drwxr-xr-x 4 root root  30 Nov 25 16:50 objects
drwxr-xr-x 4 root root  31 Nov 25 16:50 refs

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

4. Git常规使用

01. 创建数据-提交数据

02. Git四种状态

未跟踪的 、未修改的 、 已修改的 、 暂存区

03. Git基础命令

git 改名
git mv a a.txt
git commit  -m "rename a a.txt"

git比对内容
git diff 比对工作目录和暂存区的不同
git diff --cached 比对暂存区和本地仓库的不同

git 查看历史操作信息
git  log 查看历史提交记录
git log --oneline 一条显示提交的历史记录
git log --oneline --decorate 查看当时的指针
git reset --hard e723hh5 回滚代码至某个版本
git reflog 查看所有的历史提交记录

1) 测试

#查看状态
[root@git git_data]# git status
# On branch master     #位于分支 master
#
# Initial commit     #初始提交
#
nothing to commit (create/copy files and use "git add" to track)    #无文件要提交(创建/拷贝文件并使用 "git add" 建立跟踪)

#创建测试文件
root@git git_data]# touch a b c
[root@git git_data]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 25 17:00 a
-rw-r--r-- 1 root root 0 Nov 25 17:00 b
-rw-r--r-- 1 root root 0 Nov 25 17:00 c

[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   a
#   b
#   c
nothing added to commit but untracked files present (use "git add" to track)

#git add a 把文件提交到了暂存区
[root@git git_data]# git add a

[root@git git_data]# ll .git/
total 16
drwxr-xr-x 2 root root   6 Nov 25 16:50 branches
-rw-r--r-- 1 root root  92 Nov 25 16:50 config
-rw-r--r-- 1 root root  73 Nov 25 16:50 description
-rw-r--r-- 1 root root  23 Nov 25 16:50 HEAD
drwxr-xr-x 2 root root 242 Nov 25 16:50 hooks
-rw-r--r-- 1 root root  96 Nov 25 17:03 index
drwxr-xr-x 2 root root  21 Nov 25 16:50 info
drwxr-xr-x 5 root root  40 Nov 25 17:03 objects
drwxr-xr-x 4 root root  31 Nov 25 16:50 refs

[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a      #发现一个新的文件
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   b
#   c

#查看提交的内容
#使用git add . 或者 * 添加目录中所有改动过的文件
[root@git git_data]# git add .
[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#   new file:   b
#   new file:   c
#

2)删除的方式

#1.只是删除缓存中的
[root@git git_data]# git rm --cached c
rm 'c'
[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#   new file:   b
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   c
#2.删除工作目录文件
[root@git git_data]# rm -f c 
[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#   new file:   b
#

或者:
#把暂停区和工作目录同时删除
[root@git git_data]# git rm -f b
rm 'b'
[root@git git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#

3)文件比对

#提交到本地仓库
[root@git git_data]# git commit -m "create a b"
[master (root-commit) da7ed82] create a b
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a
 create mode 100644 b
[root@git git_data]# git status
# On branch master
nothing to commit, working directory clean

#文件比对
[root@git git_data]# echo '1111' >>a
#比对工作目录跟暂存区的不同
[root@git git_data]# git diff a 
diff --git a/a b/a
index e69de29..5f2f16b 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+1111
#提交到暂存区后,再次比对,没有变化
[root@git git_data]# git add .
[root@git git_data]# git diff a 
#比对暂存区和本地仓库的区别
[root@git git_data]# git diff --cached
diff --git a/a b/a
index e69de29..5f2f16b 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+1111
#提交到本地仓库
[root@git git_data]# git commit -m 'modify a'
[master e20b4f9] modify a
 1 file changed, 1 insertion(+)
 #再次比对,没有变化
[root@git git_data]# git diff --cached

4)覆盖

#暂存区中的内容覆盖到工作目录
[root@git git_data]# echo 2222 >> a
[root@git git_data]# git diff a 
diff --git a/a b/a
index 5f2f16b..4f142ee 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 1111
+2222
#从暂存区中的文件覆盖到工作目录
[root@git git_data]# git checkout -- a
[root@git git_data]# git diff
[root@git git_data]# cat a
1111

5)代码已经添加到暂存区,发现写错了

[root@git git_data]# echo 2222 >> a
[root@git git_data]# git add .
[root@git git_data]# git diff a
[root@git git_data]# cat a
1111
2222
[root@git git_data]# git diff --cached
diff --git a/a b/a
index 5f2f16b..4f142ee 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 1111
+2222
#将本地仓库的文件覆盖暂存区
[root@git git_data]# git reset HEAD a
Unstaged changes after reset:
M   a
[root@git git_data]# git diff --cached
[root@git git_data]# git diff 
diff --git a/a b/a
index 5f2f16b..4f142ee 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 1111
+2222

[root@git git_data]# git checkout a
[root@git git_data]# git diff 
[root@git git_data]# cat a
1111

6) 将代码提交到本地仓库,发现写错了

#将代码提交到本地仓库,发现写错了
[root@git git_data]# echo 2222 >> a
[root@git git_data]# git commit -am "modify 222 a"
[master b7818c5] modify 222 a
 1 file changed, 1 insertion(+)
[root@git git_data]# git diff
[root@git git_data]# git diff --cached
[root@git git_data]# cat a
1111
2222
#查看git的提交记录
#用1行内容来表示记录
[root@git git_data]# git log --oneline
b7818c5 modify 222 a
e20b4f9 modify a
da7ed82 create a b
#显示提交的指针方向
[root@git git_data]# git log --oneline --decorate
b7818c5 (HEAD, master) modify 222 a
e20b4f9 modify a
da7ed82 create a b
[root@git git_data]# cat a
1111
2222
[root@git git_data]# echo 3333 >> a
[root@git git_data]# git commit -am "modify 3333 a"
[master c949243] modify 3333 a
 1 file changed, 1 insertion(+)
[root@git git_data]# git log --oneline --decorate
c949243 (HEAD, master) modify 3333 a
b7818c5 modify 222 a
e20b4f9 modify a
da7ed82 create a b
[root@git git_data]# git reset --hard da7ed82
HEAD is now at da7ed82 create a b
[root@git git_data]# cat a

#突然发现恢复错了
[root@git git_data]# git reflog --oneline
da7ed82 HEAD@{0}: reset: moving to da7ed82
c949243 HEAD@{1}: commit: modify 3333 a
b7818c5 HEAD@{2}: commit: modify 222 a
e20b4f9 HEAD@{3}: commit: modify a
da7ed82 HEAD@{4}: commit (initial): create a b
[root@git git_data]# git reset --hard b7818c5
HEAD is now at b7818c5 modify 222 a
[root@git git_data]# cat a
1111
2222

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

[root@git ~/git_data]# git reset --hard 18c89e4
HEAD is now at 18c89e4 modified a

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

打开发现回退错了,应该回退到bbb版本

#打开发现回退错了,应该回退到bbb版本
[root@git ~/git_data]# cat aaaa
#这时候查看log没有commit bbb的历史了
[root@git ~/git_data]# git log --oneline
18c89e4 modified a
1a85735 rename a.txt
a64e0f41 commit a.txt
c6b0ac2 new file a

竟然没有了add bbb这个提交版本记录?原因很简单,因为我们当前的工作版本是历史的一个提交点,这个历史提交点还没有发生过add bbb 更新记录,所以当然就看不到了,要是想"还原到未来"的历史更新点,可以用git reflog命令来查看所有的历史记录:

#使用git reflog 可查看总历史内容
[root@git ~/git_data]# git reflog
18c89e4 HEAD@{0}: reset: moving to 18c89e4
cc5c366 HEAD@{1}: commit: add ccc
6118c8d HEAD@{2}: commit: add bbb
18c89e4 HEAD@{3}: commit: modified a
1a85735 HEAD@{4}: commit: rename a.txt
a64e0f41 HEAD@{5}: commit: commit a.txt
c6b0ac2 HEAD@{6}: commit (initial): new file a
#然后使用reset回到bbb的版本内容下
[root@git ~/git_data]# git reset --hard 6118c8d
HEAD is now at 6118c8d add bbb
[root@git ~/git_data]# cat a
aaa
bbb

猜你喜欢

转载自www.cnblogs.com/gongjingyun123--/p/11969713.html
今日推荐