git笔记
devops介绍
Devops是什么
解决开发与运维之间的冲突
Devops能干什么
提高产品质量
1 自动化测试
2 持续集成
3 代码质量管理工具
4 程序员鼓励师
Devops如何实现
既然这么好?为什么有些公司没有
设计架构规划-代码的存储-构建-测试、预生产、部署、监控
Git版本控制系统
.版本控制系统简介
vcs
version control system
版本控制系统是一种记录一个或若干个文件内容变化,以便将来查阅特定版本内容情况的系统
记录文件的所有历史变化
随时可恢复到任何一个历史状态
多人协作开发
为什么需要版本控制系统
常见版本管理工具
git
git安装和基础命令的使用
准备环境
主机名 gitlab
主机 p 10.0.0.200
内存 2.5G
磁盘 50G
环境检测
1 查看系统版本(centos6或者centos7都可以)
[root@gitlab ~] # cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
2 查看内核
[root@gitlab ~] # uname -r
3.10.0-957.el7.x86_64
3 确认关闭selinux
[root@gitlab ~] # getenforce
Disabled
4 关闭防火墙
[root@gitlab ~] # systemctl stop firewalld
[root@gitlab ~] # systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
git安装部署
1 安装git
[root@gitlab ~] # yum install git
2 查看参数
[root@gitlab ~] # git config
--global 使用全局配置文件
--system 使用系统级配置文件
--local 使用版本库级配置文件
3 配置git使用用户
[root@gitlab ~] # git config --global user.name "dayu"
4 配置git使用邮箱
[root@gitlab ~] # git config --global user.email "dayu@qq.com"
5 语法高亮
[root@gitlab ~] # git config --global color.ui true
6 查看配置(方法一)
[root@gitlab ~] # git config --list
user.name=dayu
user.email=dayu@qq.com
color.ui=true
7 查看配置(方法二)
[root@gitlab ~] # cat .gitconfig
[user]
name = dayu
email = dayu@qq.com
[color]
ui = true
git初始化
1 初始化工作目录、对已存在的目录或者对已存在的目录都可进行初始化
[root@gitlab ~] # mkdir git_data
[root@gitlab ~] # cd git_data/
2 初始化
[root@gitlab git_data] # git init
3 查看工作区状态
[root@gitlab git_data] # git status
隐藏文件介绍:
branches # 分支目录
config # 定义项目特有的配置选项
description # 仅供git web程序使用
HEAD # 指示当前的分支
hooks # 包含git钩子文件
info # 包含一个全局排除文件(exclude文件)
objects # 存放所有数据内容,有info和pack两个子文件夹
refs # 存放指向数据(分支)的提交对象的指针
index # 保存暂存区信息,在执行git init的时候,这个文件还没有
4 查看本地仓库
[root@gitlab git_data] # ll .git/objects/
total 0
drwxr-xr-x 2 root root 6 May 2 13:43 info
drwxr-xr-x 2 root root 6 May 2 13:43 pack
git常规使用
1. 创建数据-提交数据
2. git四种状态
3. git基础命令
1 查看Git状态
[root@git git_data]# git status
# 位于分支 master
# 初始提交
无文件要提交(创建/拷贝文件并使用 "git add" 建立跟踪)
2 创建一个文件
[root@gitlab git_data] # touch a.txt
[root@gitlab git_data] # git status
# On branch master
#
# Initial commit
#
# Untracked files: (未被跟踪的文件)
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
nothing added to commit but untracked files present (use "git add" to track)
3 把文件提交到暂存区
[root@gitlab git_data] # git add a.txt
4 生成一个index
[root@gitlab git_data] # ll .git/index
-rw-r--r-- 1 root root 104 May 2 13:56 .git/index
5 暂存区有a.txt
[root@gitlab git_data] # git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage) (文件可以删除)
#
# new file: a.txt
#
6 在暂存区删除文件 并不是真正的删除,而是把文件退回到工作区
[root@gitlab git_data] # git rm --cached a.txt
7 查看状态得知 在暂存区删除文件 并不是真正的删除,而是把文件退回到工作区
[root@gitlab git_data] # git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
nothing added to commit but untracked files present (use "git add" to track)
8 提交到本地仓库
[root@gitlab git_data] # git add a.txt
[root@gitlab git_data] # git commit -m "newfile a.txt"
9 文件彻底被仓库管理
[root@gitlab git_data] # git status
# On branch master
nothing to commit, working directory clean
改名
方法一:(未成功)
1 移动文件a.txt 到 a
[root@gitlab git_data] # mv a.txt a
2 显示不小心给删除了
[root@gitlab git_data] # git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
no changes added to commit (use "git add" and/or "git commit -a")
3 拉回a.txt
[root@gitlab git_data] # git checkout -- a.txt
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 13:50 a
-rw-r--r-- 1 root root 0 May 2 14:05 a.txt
4 删除a
[root@gitlab git_data] # rm -rf a
5 显示没有什么可以提交的 说明改命未成功
[root@gitlab git_data] # git status
# On branch master
nothing to commit, working directory clean
方法二(成功)
1 Git改名
[root@gitlab git_data] # git mv a.txt a
2 查看状态
[root@gitlab git_data] # git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: a.txt -> a
#
3 添加改名信息
[root@gitlab git_data] # git add .
4 提交改名信息
[root@gitlab git_data] # git commit -m "renamed a.txt a"
[master 215926d] renamed a.txt a
1 file changed, 0 insertions(+), 0 deletions(-)
rename a.txt => a (100%)
删除文件
1 创建一个b文件
[root@gitlab git_data] # touch b.txt
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 14:05 a
-rw-r--r-- 1 root root 0 May 2 14:11 b.txt
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 14:05 a
-rw-r--r-- 1 root root 0 May 2 14:11 b.txt
2 添加到暂存区
[root@gitlab git_data] # git add .
3 提交到仓库
[root@gitlab git_data] # git commit -m "newfile b.txt"
[master 8c2992d] newfile b.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
4 删除文件
[root@gitlab git_data] # git rm -f b.txt
rm 'b.txt'
5 添加到暂存区
[root@gitlab git_data] # git add .
6 提交到仓库
[root@gitlab git_data] # git commit -m "del b.txt"
[master d8916ea] del b.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 b.txt
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 14:05 a
PS: 并没有真正的删除,可以恢复快照,没一个commit就是一个快照
Git log
查看历史镜像
1 第一种显示方式
[root@gitlab git_data] # git log
[master d8916ea] del b.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 b.txt
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 14:05 a
[root@gitlab git_data] # git log
commit d8916ea40d05c7d0129ad09a8bbdfb1390f4f147
Author: dayu <dayu@qq.com>
Date: Sat May 2 14:17:00 2020 +0800
del b.txt
2 第二种显示方式
[root@gitlab git_data] # git log --oneline
d8916ea del b.txt
8c2992d newfile b.txt
215926d renamed a.txt a
c71ae3a newfile a.txt
3 第三种显示方式 指针指向那个版本
[root@gitlab git_data] # git log --oneline --decorate
d8916ea (HEAD, master) del b.txt
8c2992d newfile b.txt
215926d renamed a.txt a
c71ae3a newfile a.txt
比对两个文件的内容
1 在linux中
[root@gitlab ~] # diff anaconda-ks.cfg oo.txt
2 在代码仓库中
比对当前工作区和暂存区有什么不同
[root@gitlab git_data] # echo oldboy >> a
[root@gitlab git_data] # git diff
diff --git a/a b/a
index 9daeafb..21ff0ad 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
test
+oldboy
3 对比暂存区和仓库
[root@gitlab git_data] # git add .
[root@gitlab git_data] # git diff --cached
diff --git a/a b/a
index 9daeafb..21ff0ad 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
test
+oldboy
4 信息提交后 全都一样
[root@gitlab git_data] # git commit -m "mofified a.txt"
[master 8c567a3] mofified a.txt
1 file changed, 1 insertion(+)
[root@gitlab git_data] # git diff
恢复历史数据
1 查看历史commit
[root@gitlab git_data] # git log --oneline
8c567a3 mofified a.txt
3815fe7 modified a>test
d8916ea del b.txt
8c2992d newfile b.txt
215926d renamed a.txt a
c71ae3a newfile a.txt
2 回滚到指定的快照
[root@gitlab git_data] # git reset --hard 8c2992d
HEAD is now at 8c2992d newfile b.txt
3 查看
[root@gitlab git_data] # ll
total 0
-rw-r--r-- 1 root root 0 May 2 14:39 a
-rw-r--r-- 1 root root 0 May 2 14:39 b.txt
4 查看所有的历史
[root@gitlab git_data] # git reflog
8c2992d HEAD@{0}: reset: moving to 8c2992d
8c567a3 HEAD@{1}: commit: mofified a.txt
3815fe7 HEAD@{2}: commit: modified a>test
d8916ea HEAD@{3}: commit: del b.txt
8c2992d HEAD@{4}: commit: newfile b.txt
215926d HEAD@{5}: commit: renamed a.txt a
c71ae3a HEAD@{6}: commit (initial): newfile a.txt
5 回滚到指定的快照
[root@gitlab git_data] # git reset --hard 3815fe7
HEAD is now at 3815fe7 modified a>test
6 查看
[root@gitlab git_data] # ll
total 4
-rw-r--r-- 1 root root 0 May 2 14:42 1.txt
-rw-r--r-- 1 root root 5 May 2 14:42 a
Git分支
`分支即是平行空间,假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到git仓库中,又有可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫”拍照功 能”的分支,这种分支只会属于你自己,而其他人看不到,等代码编写完成后再与原来的项目主分支合并下即可,这样即能保证代码不丢失,又不影响其他人的工作。
1 查看分支
[root@gitlab git_data] # git branch
* master
2 查看默认指到哪个分支
[root@gitlab git_data] # git log --oneline --decorate
3815fe7 (HEAD, master) modified a>test
3 创建分支
[root@gitlab git_data] # git branch testing
4 查看创建是否成功
[root@gitlab git_data] # git branch
* master
testing
5 切换分支
[root@gitlab git_data] # git checkout testing
Switched to branch 'testing'
6 查看切换是否成功
[root@gitlab git_data] # git branch
master
* testing
PS *在哪 哪个就是主分支
7 切换到主分支
[root@gitlab git_data] # git checkout master
8 删除分支
[root@gitlab git_data] # git branch -d testing
Deleted branch testing (was 3815fe7).
9 创建并切换到testing分支
[root@gitlab git_data] # git checkout -b testing
Switched to a new branch 'testing'
10 合并分支
a 在当前testing分支a文件里面添加testing 并提交
[root@gitlab git_data] # echo testing >>a
[root@gitlab git_data] # git add .
[root@gitlab git_data] # git commit -m "modifief a > testing"
b 切换到master分支
[root@gitlab git_data] # git checkout master
Switched to branch 'master'
c 查看文件a并没有resting,并且指针指向最后一次操作
[root@gitlab git_data] # cat a
test
[root@gitlab git_data] # git log --oneline --decorate
3815fe7 (HEAD, master) modified a>test
d8916ea del b.txt
8c2992d newfile b.txt
215926d renamed a.txt a
c71ae3a newfile a.txt
d 合并分支
[root@gitlab git_data] # git merge testing
Updating 3815fe7..22be157
Fast-forward
a | 1 +
1 file changed, 1 insertion(+)
[root@gitlab git_data] # cat a
test
testing
[root@gitlab git_data] # git log --oneline --decorate
22be157 (HEAD, testing, master) modifief a > testing
3815fe7 modified a>test
d8916ea del b.txt
8c2992d newfile b.txt
215926d renamed a.txt a
c71ae3a newfile a.txt
e 删除分支 防止主分支落后 所以每次创建分支都要删除
[root@gitlab git_data] # git branch -d testing
代码冲突问题
1 首先创建一个分支(分支会克隆主分支)
[root@gitlab git_data] # git branch testing
2 当前分支为master
[root@gitlab git_data] # git branch
* master
testing
3 在master分支文件内写入alex
[root@gitlab git_data] # echo alex >>a
[root@gitlab git_data] # git add .
[root@gitlab git_data] # git commit -m "modified alex >a"
[master 4ff8a6f] modified alex >a
1 file changed, 1 insertion(+)
4 切换分支
[root@gitlab git_data] # git checkout testing
Switched to branch 'testing'
[root@gitlab git_data] # cat a
test
testing
lidao
lixin
5 在创建的testing分支文件里写入内容
[root@gitlab git_data] # echo dayu>a
[root@gitlab git_data] # git add .
[root@gitlab git_data] # git commit -m "modoified dayu>a"
[testing 816cf38] modoified dayu>a
1 file changed, 1 insertion(+), 4 deletions(-)
6 切换到主分支
[root@gitlab git_data] # git checkout master
Switched to branch 'master'
7 合并分支,出现问题
[root@gitlab git_data] # git merge testing
Auto-merging a
CONFLICT (content): Merge conflict in a
Automatic merge failed; fix conflicts and then commit the result.
8 手动编辑文件 把没用的删掉
[root@gitlab git_data] # vim a
<<<<<<< HEAD
test
testing
lidao
lixin
alex
=======
dayu
>>>>>>> testing
[root@gitlab git_data] # git add .
[root@gitlab git_data] # git commit -m "merge testing"
[master ffabb5a] merge testing
[root@gitlab git_data] # git status
# On branch master
nothing to commit, working directory clean
[root@gitlab git_data] # cat a
test
testing
lidao
lixin
alex
dayu
git tag
1 给当前环境打标签
[root@gitlab git_data] # git tag -a v2.0 -m "v2.0稳定版"
2 查看打标签的内容
[root@gitlab git_data] # git show v2.0
tag v2.0
Tagger: dayu <dayu@qq.com>
Date: Sat May 2 15:29:22 2020 +0800
v2.0稳定版
commit ffabb5aaa855a008cbbceb8da8e312ce34b84ad6
Merge: 4ff8a6f 816cf38
Author: dayu <dayu@qq.com>
Date: Sat May 2 15:26:02 2020 +0800
merge testing
diff --cc a
index 6c3609a,919582d..d3a7863
--- a/a
+++ b/a
@@@ -1,5 -1,1 +1,6 @@@
+test
+testing
+lidao
+lixin
+alex
+ dayu
3 给刚安装好的时候打标签
[root@gitlab git_data] # git reflog
[root@gitlab git_data] # git reset --hard c71ae3a
HEAD is now at c71ae3a newfile a.txt
[root@gitlab git_data] # git tag -a v1.0 -m "v1.0稳定版"
4 查看打的标签
[root@gitlab git_data] # git tag
v1.0
v2.0
5 回滚到版本2.0
[root@gitlab git_data] # git reset --hard v2.0
HEAD is now at ffabb5a merge testing
6 指定某一次的提交为标签
[root@git git_data]# git tag -a v2.0 dbead4c -m "add bbb version v2.0"
7 删除标签
[root@gitlab git_data] # git tag -d v1.0
Deleted tag 'v1.0' (was 2dd10e2)
PS 在master主干打标签