Devops 02 Git

1. DevOps介绍

铁三角
开发  测试   运维   

项目周期、产品周期 
老板的想法 产品经理的构造 开发的代码实现 测试的功能测试 运维平台构建 代码的上线

开发、测试	  变化、代码的更新
运维			稳定、网站能够正常运行下去

2. 版本控制系统

vcs 【版本控制系统的英文单词缩写`version control system`#版本控制系统作用
1、记录文件的所有的历史变化
2、随时可以恢复到任何一个历史状态
3、多人进行协作开发

#常见的版本管理工具
Git 可以搭建本地仓库,也可以在全国各地搭建
SVN 集中式的版本控制,只有一个中央数据库,一旦数据库挂了,所有人就都没办法使用SVN了。

3. Git的安装与应用

1、环境准备
#系统版本
[root@git ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
#内核版本
[root@git ~]# uname -r
3.10.0-957.el7.x86_64
#关闭selinux
setenforce 0
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@git ~]# getenforce			#查看selinux状态 Disabled 就是关闭状态
#关闭防火墙firewalld
[root@git ~]# iptables-save 		#命令执行没有输出结果,表示防火墙是关闭的
systemctl disable firewalld
systemctl stop firewalld

2、安装
yum install -y git

[root@git ~]# git config
usage: git config [options]

Config file location
    --global              use global config file      			#全局
    --system              use system config file				#系统级别配置
    --local               use repository config file			#使用版本库级别配置
3、git 全局配置
git config --global  user.name  "qls"				#配置Git使用用户
git config --global  user.email  "123@qq.com"		#配置用户的邮箱
git config --global  color.ui  true				#语法高亮
git config  --list								#显示配置列表
user.name=qls
user.email=123@qq.com
color.ui=true
[root@git ~]# ll -a 
-rw-r--r--   1 root root   58 2020-03-10 10:49 .gitconfig
[root@git ~]# cat .gitconfig 
[user]
	name = qls
	email = 123@qq.com
[color]
	ui = true

4、Git 初始化【创建一个工作目录,再初始化】
[root@git ~]# mkdir  git_data
[root@git ~]# cd git_data
[root@git ~/git_data]# ll
total 0
[root@git ~/git_data]# git init		#初始化工作目录
Initialized empty Git repository in /root/git_data/.git/
[root@git ~/git_data]# ll .git/
total 12
drwxr-xr-x 2 root root   6 2020-03-10 10:53 branches      #分支目录
-rw-r--r-- 1 root root  92 2020-03-10 10:53 config		  #特有的配置选项
-rw-r--r-- 1 root root  73 2020-03-10 10:53 description	  #Git web程序使用的
-rw-r--r-- 1 root root  23 2020-03-10 10:53 HEAD		  #指示当前的分支
drwxr-xr-x 2 root root 242 2020-03-10 10:53 hooks		  #Git的钩子文件
drwxr-xr-x 2 root root  21 2020-03-10 10:53 info		  #全局排除文件(exclude文件)
drwxr-xr-x 4 root root  30 2020-03-10 10:53 objects		  #存放所有数据 info pack
drwxr-xr-x 4 root root  31 2020-03-10 10:53 refs		  #存放指向数据分支的提交的对象的指针
											index		  #保存暂存区信息 

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

4. Git常规使用

# Git 的一家子  
工作目录 暂存区域 本地仓库 远程仓库

# Git 的四种状态:
Untracked   未跟踪
Unmodified	未修改
Modified	已修改
Staged		已暂存

# Git 的基础命令
git status		#显示当前工作区的状态

#创建一些测试文件
[root@git ~/git_data]# touch  test.txt
[root@git ~/git_data]# touch  oldboy.txt
[root@git ~/git_data]# touch  oldgirl.txt
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:			#发现未跟踪的文件
#   (use "git add <file>..." to include in what will be committed)
#
#	oldboy.txt
#	oldgirl.txt
#	test.txt
nothing added to commit but untracked files present (use "git add" to track)

#文件提交到暂存区
[root@git ~/git_data]# git add test.txt		#将文件提交到暂存区
[root@git ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   test.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	oldboy.txt
#	oldgirl.txt

[root@git ~/git_data]# git add .  		#添加所有的文件到暂存区或者使用 git add * 命令
[root@git ~/git_data]# git rm --cached test.txt		#从暂存区将文件删除
[root@git ~/git_data]# rm -f test.txt 				#删除工作目录中的文件

#删除文件两种方法
1. 先删除暂存区里面的文件,在删除工作目录中的文件
git rm --cached test.txt
rm -f test.txt 
2. 直接从暂存区连同工作目录中的文件删除
[root@git ~/git_data]# git rm -f oldgirl.txt

#从暂存区提交到本地仓库
[root@git ~/git_data]# git commit -m "new 3 file"

#文件重命名两种方法
方法一
1. 本地重命名,修改工作目录中文件的名称
[root@git ~/git_data]# mv a.txt  aaa.txt
2. 删除暂存区中的文件
[root@git ~/git_data]# git rm --cached a.txt
3. 将重命名好的文件提交到暂存区
[root@git ~/git_data]# git add .
4. 提交到本地仓库
[root@git ~/git_data]# git commit -m "mv a.txt  aaa.txt"

方法二
1、直接使用git进行重命名
[root@git ~/git_data]# git mv b.txt bbb.txt
2、提交到本地仓库
[root@git ~/git_data]# git commit -m "mv b.txt bbb.txt"

#文件内容比对,比对工作目录与暂存区文件内容不同之处
[root@git ~/git_data]# echo "111111111" >>oldboy.txt 	#先给工作目录中文件添加新内容
[root@git ~/git_data]# git diff oldboy.txt
diff --git a/oldboy.txt b/oldboy.txt
index e69de29..bb81b3c 100644
--- a/oldboy.txt
+++ b/oldboy.txt
@@ -0,0 +1 @@
+111111111
[root@git ~/git_data]# git add oldboy.txt	#将文件提交到暂存区
[root@git ~/git_data]# git diff oldboy.txt	#再次进行比对发现,暂存区与工作目录的文件内容相同

#比对暂存区文件内容跟本地仓库文件内容不同之处
[root@git ~/git_data]# git diff --cached oldboy.txt
[root@git ~/git_data]# git commit -m "modify oldboy.txt"		#提交到本地仓库


#同时提交到了 本地仓库 和 暂存区
[root@git ~/git_data]# echo "2222222222"  >> oldboy.txt
[root@git ~/git_data]# git commit -am "modify oldboy.txt 2"

#显示Git的历史操作记录
[root@git ~/git_data]# git log
[root@git ~/git_data]# git reflog					#查看所有历史操作记录

#使用一行来显示Git的历史记录
[root@git ~/git_data]# git log --oneline
d3afca4 modify oldboy.txt 3
4d83f58 modify oldboy.txt 2

#显示当前指针所指向的分支
[root@git ~/git_data]# git log --oneline --decorate

#显示具体内容的变化
[root@git ~/git_data]# git log -p

#显示最近的一次历史记
[root@git ~/git_data]# git log -1

#恢复历史数据
1.改变了工作区中文件的内容,发现改错了。
[root@git ~/git_data]# echo "444444444" >>oldboy.txt 
[root@git ~/git_data]# git status
[root@git ~/git_data]# cat oldboy.txt 

[root@git ~/git_data]# git checkout -- oldboy.txt		#将暂存区里面的内容覆盖到工作目录
[root@git ~/git_data]# git status
[root@git ~/git_data]# cat oldboy.txt 
	#误删除文件,从暂存区拉回
[root@git ~/git_data]# rm -f aaa.txt 
[root@git ~/git_data]# git checkout aaa.txt				#将暂存区里面的内容覆盖到工作目录,-- 可以不加

2. 工作目录和暂存区都发生了改变,没有提交到本地仓库,发现改错了
[root@git ~/git_data]# git diff --cached oldboy.txt
	#先从本地仓库覆盖暂存区
[root@git ~/git_data]# git reset HEAD oldboy.txt
	#再从暂存区覆盖工作目录
[root@git ~/git_data]# git checkout -- oldboy.txt

3.修改了工作区,暂存区,也提交到了本地仓库,发现写错了
[root@git ~/git_data]# echo "44444444" >> oldboy.txt 
[root@git ~/git_data]# git add .
[root@git ~/git_data]# git commit -m "modify oldboy.txt 4 "

	#通过历史记录来恢复数据
[root@git ~/git_data]# git reflog					#查看所有历史操作记录
[root@git ~/git_data]# git log --oneline			#当前分支历史记录
ef74f82 modify oldboy.txt 4
d3afca4 modify oldboy.txt 3
4d83f58 modify oldboy.txt 2
4bc8f84 modify oldboy.txt
6e588a7 mv b.txt  bbb.txt
ed36a6a mv a.txt  aaa.txt
74809a8 new 3 file
[root@git ~/git_data]# git status
[root@git ~/git_data]# git reset --hard d3afca4		#将所有地方的数据恢复到这个快照
HEAD is now at d3afca4 modify oldboy.txt 3
[root@git ~/git_data]# cat oldboy.txt 
[root@git ~/git_data]# git diff oldboy.txt
[root@git ~/git_data]# git diff --cached oldboy.txt

#发现刚才恢复错了
[root@git ~/git_data]# git log  --oneline	#查看日志时,发现没有了modify 4那一次的修改了
d3afca4 modify oldboy.txt 3
4d83f58 modify oldboy.txt 2
4bc8f84 modify oldboy.txt
6e588a7 mv b.txt  bbb.txt
ed36a6a mv a.txt  aaa.txt
74809a8 new 3 file
[root@git ~/git_data]# git reflog		#显示所有的git历史记录
d3afca4 HEAD@{0}: reset: moving to d3afca4
ef74f82 HEAD@{1}: commit: modify oldboy.txt 4
d3afca4 HEAD@{2}: commit: modify oldboy.txt 3
4d83f58 HEAD@{3}: commit: modify oldboy.txt 2
4bc8f84 HEAD@{4}: commit: modify oldboy.txt
6e588a7 HEAD@{5}: commit: mv b.txt bbb.txt
ed36a6a HEAD@{6}: commit: mv a.txt aaa.txt
74809a8 HEAD@{7}: commit (initial): new 3 file
[root@git ~/git_data]# git reset --hard ef74f82		#恢复记录
HEAD is now at ef74f82 modify oldboy.txt 4
[root@git ~/git_data]# cat oldboy.txt 

#git分支

#显示你当前的指针指向那个分支
[root@git ~/git_data]# git log --oneline --decorate
ef74f82 (HEAD, master) modify oldboy.txt 4
d3afca4 modify oldboy.txt 3
4d83f58 modify oldboy.txt 2
4bc8f84 modify oldboy.txt
6e588a7 mv b.txt  bbb.txt
ed36a6a mv a.txt  aaa.txt
74809a8 new 3 file

#创建分支
[root@git ~/git_data]# git branch test

#显示当前所在的分支
[root@git ~/git_data]# git branch 
* master			#*指向当前所在的分支
  test

#切换分支
[root@git ~/git_data]# git checkout test	#切换分支
[root@git ~/git_data]# git branch			#查看当前分支【* 号在哪个分支,哪个就是当前分支】
  master
* test

[root@git ~/git_data]# ll					#查看test分支下,工作目录内的文件
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt

[root@git ~/git_data]# touch test.txt		#在test分支下的工作目录内创建新文件
[root@git ~/git_data]# git add .			#将工作目录内的新文件放到暂存区
[root@git ~/git_data]# git commit -m "test commit"	#将暂存区的内容放入本地仓库

[root@git ~/git_data]# git log --oneline --decorate		#查看修改的历史记录
[root@git ~/git_data]# git checkout master	#切换到主分支
[root@git ~/git_data]# ll					#查看主分支下,工作目录内的文件
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt

#合并分支
[root@git ~/git_data]# git merge test	#在主分支合并test分支
[root@git ~/git_data]# ll				#合并分支之后,test分支下创建的test.txt文件就显示出来了
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:19 test.txt

[root@git ~/git_data]# touch master.txt		#创建文件
[root@git ~/git_data]# git add .			#将文件从工作目录放入暂存区
[root@git ~/git_data]# git commit -m "master touch master.txt"	#将文件从暂存区放入本地仓库

[root@git ~/git_data]# git checkout test	#切换到test分支
[root@git ~/git_data]# ll
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:19 test.txt
[root@git ~/git_data]# git merge master		#在test分支合并主分支
[root@git ~/git_data]# ll					#查看合并主分支后test分支工作目录内容
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:23 master.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:19 test.txt

#合并冲突
[root@git ~/git_data]# git branch	#查看当前分支什么【主分支】
* master
  test
[root@git ~/git_data]# ll			#查看主分支工作目录的内容
total 4
-rw-r--r-- 1 root root  0 2020-03-10 15:05 aaa.txt
-rw-r--r-- 1 root root  0 2020-03-10 12:06 bbb.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:23 master.txt
-rw-r--r-- 1 root root 41 2020-03-10 15:21 oldboy.txt
-rw-r--r-- 1 root root  0 2020-03-10 16:19 test.txt
[root@git ~/git_data]# echo "aaaaaaaaaa" >>oldboy.txt	#修改主分支工作目录下文件内容
[root@git ~/git_data]# git commit -am "modify master"	#将修改后的文件传入主分支的暂存区和本地仓库

[root@git ~/git_data]# git checkout test	#切换到test分支
[root@git ~/git_data]# cat oldboy.txt 		#查看工作目录中文件的内容
111111111
2222222222
3333333333
44444444
[root@git ~/git_data]# echo "bbbbbbbbbb" >>oldboy.txt	#修改test分支工作目录下文件内容
[root@git ~/git_data]# git commit -am "modify test"		#将修改后的文件传入test分支的暂存区和本地仓库

[root@git ~/git_data]# git checkout master	#切换到主分支
[root@git ~/git_data]# cat oldboy.txt	#查看工作目录中文件的内容
111111111
2222222222
3333333333
44444444
<<<<<<< HEAD
aaaaaaaaaa
=======			#上面和下面内容不同产生冲突
bbbbbbbbbb
>>>>>>> test
[root@git ~/git_data]# git log --oneline --decorate		#查看分支指向
38fca18 (HEAD, master) modify master

[root@git ~/git_data]# vim oldboy.txt		#编辑文件内容
[root@git ~/git_data]# git commit -am "commit modify oldboy.txt" #将编辑后的内容传入暂存区和本地仓库
[root@git ~/git_data]# cat oldboy.txt		#查看编辑后的文件内容
111111111
2222222222
3333333333
44444444
aaaaaaaaaa
[root@git ~/git_data]# git checkout test	#切换到test分支
[root@git ~/git_data]# cat oldboy.txt		#查看test分支下工作目录的文件内容
111111111
2222222222
3333333333
44444444
bbbbbbbbbb
[root@git ~/git_data]# git merge master		#切换到主分支
[root@git ~/git_data]# cat oldboy.txt		#查看主分支下工作目录的文件内容
111111111
2222222222
3333333333
44444444
aaaaaaaaaa

#删除分支
[root@git ~/git_data]# git branch -d test	#删除分支
[root@git ~/git_data]# git branch  			#查看当前分支有哪些
* master
[root@git ~/git_data]# git reflog			#查看所有历史操作记录

# Git 标签
-a	设置标签版本
-m	设置注释
git tag -a v1.0 871669a -m "注释信息"		#指定某一次提交为标签

git tag			#查看所有标签
git show v1.0 	#查看指定标签的详细信息
git tag -d v1.0 #删除标签

#根据标签回滚
1、查看所有标签,找到你想回滚的标签
2、查看指定标签的详细信息
3、根据标签的详细信息去找 git reflog 历史记录中的你想恢复的信息
4、回滚 git reset --hard 568ec7a

5. Git命令精简

# Git 的一家子  
工作目录 暂存区域 本地仓库 远程仓库

# Git 的四种状态:
Untracked   未跟踪
Unmodified	未修改
Modified	已修改
Staged		已暂存

# Git 的基础命令
git status					#显示当前工作区的状态
# Untracked files:			#发现未跟踪的文件
# Changes to be committed:	#文件已经提交到暂存区

#文件提交到暂存区
git add test.txt				#添加单个文件test.txt到暂存区
git add .  						#添加所有的文件到暂存区或者使用 git add * 命令
#从暂存区提交到本地仓库
git commit -m "new 3 file"
#同时提交到了 本地仓库 和 暂存区【只有修改文件内容时才可以同时提交】
echo "2222222222"  >> oldboy.txt
git commit -am "modify oldboy.txt 2"

#删除文件-方法一
git rm -f test.txt				#直接从暂存区连同工作目录中的文件删除
#删除文件-方法二
git rm --cached test.txt		#从暂存区将文件删除
rm -f test.txt					#删除工作目录中的文件

#文件重命名-方法一
git mv b.txt bbb.txt				#直接使用git进行重命名
git commit -m "mv b.txt bbb.txt"	#提交到本地仓库
#文件重命名-方法二
mv a.txt aaa.txt					#本地重命名,修改工作目录中文件的名称
git rm --cached a.txt				#删除暂存区中的文件
git add .							#将重命名好的文件提交到暂存区
git commit -m "mv a.txt  aaa.txt"	#提交到本地仓库

#文件内容比对
git diff oldboy.txt					#比对工作目录与暂存区文件内容不同之处
git diff --cached oldboy.txt		#比对暂存区与本地仓库文件内容不同之处

#显示Git的历史操作记录
git reflog							#查看所有历史操作记录
git log								#查看当前的历史记录
git log --oneline					#使用一行来显示Git的历史记录
d3afca4 	modify oldboy.txt 3
标签			注释
4d83f58 	modify oldboy.txt 2
git log --oneline --decorate		#显示当前指针所指向的分支,显示上传或修改文件是哪个分支
git log -p							#显示具体内容的变化
git log -1							#显示最近的一次历史记录,也可以根据数字定义显示最近几次

#恢复历史数据
git checkout -- aaa.txt				#将暂存区里面的内容覆盖到工作目录
git checkout aaa.txt				#将暂存区里面的内容覆盖到工作目录,-- 可以不加
git reset HEAD oldboy.txt			#从本地仓库覆盖暂存区
#通过历史记录来恢复数据
git reflog							#查看所有历史操作记录
git log --oneline					#查看当前分支历史记录
git reset --hard d3afca4			#将所有地方的数据恢复到这个快照

git branch							#显示当前所在的分支
* master							#查看当前分支【* 号在哪个分支,哪个就是当前分支】
  test
git branch test						#创建Git分支
git checkout test					#切换分支
git branch -d test					#删除分支
git merge test						#在主分支合并test分支
git merge master					#在test分支合并主分支

#合并冲突解决方法【同时对主分支和test分支相同文件进行修改,然后再合并就会出现冲突】
git checkout master					#切换到主分支
vim oldboy.txt						#编辑文件内容
git commit -am "commit modify oldboy.txt"	#将编辑后的内容传入暂存区和本地仓库

# Git 标签
-a	设置标签版本			-m	设置注释
git tag -a v1.0 871669a -m "注释信息"		#指定某一次提交为标签
git tag			#查看所有标签
git show v1.0 	#查看指定标签的详细信息
git tag -d v1.0 #删除标签
	#根据标签回滚
1、查看所有标签,找到你想回滚的标签
2、查看指定标签的详细信息
3、根据标签的详细信息去找 git reflog 历史记录中的你想恢复的信息
4、回滚 git reset --hard 568ec7a

#远程仓库
git remote						#查看远程仓库名称
origin
git remote rename origin ycck	#重命名远程仓库名称 origin 为 ycck
git remote -v					#查看远程仓库具体信息
origin	git@10.0.0.81:OPS/dzp.git (fetch)
origin	git@10.0.0.81:OPS/dzp.git (push)
git remote add ycck git@10.0.0.81:OPS/git_data.git		#增加远程仓库
git remote remove ycck			#删除远程仓库

#更新远程仓库【有时候会失败,就需要删除工作目录,再从远程仓库克隆数据】
git pull git@10.0.0.81:OPS/git_data.git		#拉取远程仓库数据到本地工作目录
rm -rf git_data								#删除工作目录
git clone git@10.0.0.81:OPS/git_data.git	#从远程仓库克隆所有数据
git push -u origin master					#推送本地工作目录内容到远程仓库

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值