Devops概念与Git版本控制系统基础

root
233
文章
0
评论
2020年2月10日23:57:00 评论 11768字阅读39分13秒

Devops概念与Git版本控制系统

Devops概念

Devops是什么?

广义上是软件生命周期内所有参与的角色

狭义上是指开发和运维共同协作

Devops能干什么?

提高产品质量

  • 自动化测试
  • 持续集成
  • 代码质量
  • 程序员鼓励师

使用Devops,新公司是最容易实现的

Git版本控制系统

Git是Devops的一种管理工具

Git能实现什么?

  • 记录文件的所有历史变化
  • 随时可以恢复到任何一个历史状态
  • 多人协作开发

分布式的版本控制系统,在每个使用者的电脑上就有一个完整的数据仓科,没有网路依然可以使用Git,当然为了团队协作,会将本地数据同步到Git服务器或者GitHub等代码仓库

Git安装

首先查看系统版本

方法一:

[root@web01 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

方法二:

[root@web01 ~]# hostnamectl
   Static hostname: web01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: bbbc68ece7ce4fe59a3e324c1855f607
           Boot ID: e4c87f34b880493c86e876d1747463c7
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.el7.x86_64
      Architecture: x86-64

检查系统是否有安装Git

[root@web01 ~]# rpm -qa |grep git

没有就进行安装

[root@kk ~]# yum -y install git
[root@kk ~]# rpm -qa|grep git
git-1.8.3.1-21.el7_7.x86_64

git config

--global       使用全局的配置文件

--system       使用系统的配置文件

--local           使用版本库级配置文件

我们使用全局的

配置git使用用户

[root@kk ~]# git config --global user.name 'kk'

配置用户邮箱

[root@kk ~]# git config --global user.email '30772818@qq.com'

显示高亮

[root@kk ~]# git config --global color.ui true

然后配置好会有一个隐藏文件记录信息

[root@kk ~]# ll -a
-rw-r--r-- 1 root root 62 Feb 10 14:27 .gitconfig
[root@kk ~]# cat .gitconfig 
[user]
	name = kk
	email = 30772818@qq.com
[color]
	ui = true

Git初始化

初始化工作目录

创建目录

[root@kk ~]# mkdir data

进入到目录初始化

[root@kk ~]# cd data/
[root@kk data]# git init
Initialized empty Git repository in /root/data/.git/
[root@kk data]# ll -a
total 12
drwxr-xr-x  3 root root 4096 Feb 10 14:36 .
dr-xr-x---. 8 root root 4096 Feb 10 14:39 ..
drwxr-xr-x  7 root root 4096 Feb 10 14:36 .git

查看git仓库状态

[root@kk data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

.Git仓库目录

[root@kk data]# ll .git/
total 32
drwxr-xr-x 2 root root 4096 Feb 10 14:36 branches
-rw-r--r-- 1 root root   92 Feb 10 14:36 config
-rw-r--r-- 1 root root   73 Feb 10 14:36 description
-rw-r--r-- 1 root root   23 Feb 10 14:36 HEAD
drwxr-xr-x 2 root root 4096 Feb 10 14:36 hooks
drwxr-xr-x 2 root root 4096 Feb 10 14:36 info
drwxr-xr-x 4 root root 4096 Feb 10 14:36 objects
drwxr-xr-x 4 root root 4096 Feb 10 14:36 refs

branches          分支目录

config               定义项目特有的配置选项

description       仅供git web程序使用

HEAD                指示当前的分支

hooks                包含git钩子文件

info                   包含一个全局排除文件(exclude)文件

objects              存放所有数据内容,有info和pack两个文件目录

refs                    存放指向数据分支的提示对象的指针

index                 保存暂存区信息,在执行git init的时候这个文件没有

工作目录就是我们创建的data

暂存区域就是index

本地仓库.git/objects/

Git工作目录内文件上传暂存区域

git add把工作目录的文件传到暂存区域

git  add 文件名

git add .git add *把工作目录内地所有文件上传到暂存区域

[root@kk data]# touch a b c
[root@kk data]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 10 19:20 a
-rw-r--r-- 1 root root 0 Feb 10 19:20 b
-rw-r--r-- 1 root root 0 Feb 10 19:20 c
[root@kk data]# git add *
[root@kk 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
#

 

Git取消上传暂存区的文件

git rm --cached 文件名      

[root@kk data]# git rm --cached a
rm 'a'
[root@kk data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   b
#	new file:   c
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	a

Git工作目录内删除文件

 

先把暂存区域的文件撤回了,再用rm -rf 删除

要出出现下面报错

git add .失败解决办法 (warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal')

git add -A

提交代码到本地仓库

git commit -m "加提示" 文件名称(什么都不写默认把所有)

[root@kk data]# git commit -m "add file a"
[master (root-commit) e16a96a] add file a
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b
 create mode 100644 c

上传到本地库就可以真正意义上管理文件了

Git文件进行改名

git mv xx xx

[root@kk data]# git mv a a.txt
[root@kk data]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 10 19:20 a.txt
-rw-r--r-- 1 root root 0 Feb 10 19:20 b
-rw-r--r-- 1 root root 0 Feb 10 19:20 c
[root@kk data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    a -> a.txt
#

Git文件比对

首先说一下两个文件的比对diff

[root@web01 data]# diff ha haa
1c1
< 123
---
> 1234

vimdiff更加直观

 

在git版本控制系统中

git diff 默认比对工作目录暂存区有什么不同

[root@web01 data]# git diff
[root@web01 data]# echo 123 >a
[root@web01 data]# git diff
diff --git a/a b/a
index e69de29..190a180 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+123

git diff  --cached默认比对暂存区和本地目录有什么不同

[root@web01 data]# git diff --cached
没信息说明暂存区域和本地目录内的文件内容一致

如果出现
[root@web01 data]# git diff --cached
diff --git a/a b/a
index e69de29..190a180 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+123
把暂存区域的内容再次提交到本地仓库
[root@web01 data]# git commit -m a
[master 9a00f20] a
1 file changed, 1 insertion(+)
[root@web01 data]# git diff --cached

 

能把版本控制系统管理的文件(也就是本地仓库已经存在),发生修改时,可以直接由工作目录到本地仓库

git commit -am "add newfile"

示例:

[root@web01 data]# echo 45 >a
[root@web01 data]# git diff
diff --git a/a b/a
index 190a180..ea90ee3 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-123
+45
[root@web01 data]# git diff --cached
[root@web01 data]# git commit -am a
[master 89f158d] a
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@web01 data]# git diff
[root@web01 data]# git diff --cached

 

Git查看历史数据

git commit 相当于虚拟机的历史镜像,任何操作都会被记录,恢复到任意时刻

git  log 查看历史的git commit

[root@web01 data]# git log
commit 89f158d66c4982b7f5c9537eeaf7c16391fdc41e
Author: kk <30772818@qq.com>
Date:   Tue Feb 11 12:48:41 2020 +0800

    a

commit 9a00f20e9e67094ed94fd5160e72de5634649865
Author: kk <30772818@qq.com>
Date:   Tue Feb 11 12:39:17 2020 +0800

    a

commit 6937c04ceabe1cbd90438bad70afe8a9417e2f86
Author: kk <30772818@qq.com>
Date:   Tue Feb 11 10:58:30 2020 +0800

    new file

 

git log --oneline  1行显示简单的commit信息

[root@web01 data]# git log --oneline
89f158d a
9a00f20 a
6937c04 new file

也就是说commit信息要写的很清楚,不然你不知道自己到底干了什么

git log --oneline --decorate   指针指向快照的内容

[root@web01 data]# git log --oneline --decorate
89f158d (HEAD, master) a
9a00f20 a
6937c04 new file

 

git log -1  只显示1条信息

git log -p  只显示1条信息

git log -1 -p 显示1条信息,显示1条信息

[root@web01 data]# git log -1 -p
commit 89f158d66c4982b7f5c9537eeaf7c16391fdc41e
Author: kk <30772818@qq.com>
Date:   Tue Feb 11 12:48:41 2020 +0800

    a

diff --git a/a b/a
index 190a180..ea90ee3 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-123
+45

 

Git恢复历史数据

git reset --rard  哈希值

[root@web01 data]# git log --oneline
89f158d a
9a00f20 a
6937c04 new file
[root@web01 data]# git log --oneline --decorate
89f158d (HEAD, master) a
9a00f20 a
6937c04 new file
[root@web01 data]# git reset --hard 6937c04
HEAD is now at 6937c04 new file
[root@web01 data]# git log --oneline --decorate
6937c04 (HEAD, master) new file

 

git  reflog要是想回到前面的快照---回到任意的commit

[root@web01 data]# git log --oneline --decorate
6937c04 (HEAD, master) new file

可以看到现在是显示不了之前信息的
[root@web01 data]# git reflog
6937c04 HEAD@{0}: reset: moving to 6937c04
89f158d HEAD@{1}: commit: a
9a00f20 HEAD@{2}: commit: a
6937c04 HEAD@{3}: commit (initial): new file

[root@web01 data]# git reset --hard 89f158d
HEAD is now at 89f158d a
让文件回来
[root@web01 data]# git log --oneline --decorate
89f158d (HEAD, master) a
9a00f20 a
6937c04 new file 

 

Git的分支

分支即是平行空间,假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到Git仓库中,又可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫"拍照功能"的分支,这种分支只会属于你自己,而其它人看不到,等代码编写完成后再与原来的项目主分支合并下即可,这样既能保证代码不丢失,又不影响其它人的工作

git branch                ----查看分支

git branch 名称        ----创建分支

[root@web01 data]# git branch testing
[root@web01 data]# git branch
* master
  testing

星号在哪就说明在哪个分支上

git checkout 分支名     ----切换分支

[root@web01 data]# git branch
* master
  testing
[root@web01 data]# git checkout testing
Switched to branch 'testing'
[root@web01 data]# git branch
  master
* testing

git checkout -b tseting  ----创建分支并切换到分支

[root@web01 data]# git checkout -b testing
Switched to a new branch 'testing'
[root@web01 data]# git checkout testing
Already on 'testing'
[root@web01 data]# git branch
master
* testing

 

删除分支

首先先切换到maste

[root@web01 data]# git branch
  master
* testing
[root@web01 data]# git checkout master
Switched to branch 'master'
[root@web01 data]# git branch
* master
  testing

 

git branch -d 名称 ----删除分支

就算删除了也能恢复

[root@web01 data]# git branch -d testing
Deleted branch testing (was 89f158d).
[root@web01 data]# git branch
* master

 

分支不会影响master主干,在分支里上传代码,切换到master主干,在工作目录里是看不见代码的,只有回到分支,才能在工作目录里看到代码

[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
[root@web01 data]# touch xx.txt
[root@web01 data]# git add xx.txt 
[root@web01 data]# git commit -m "add xx.txt"
[testing c7a04bc] add xx.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 xx.txt
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
-rw-r--r-- 1 root root 0 Feb 11 16:42 xx.txt
[root@web01 data]# git branch
  master
* testing
[root@web01 data]# git log --oneline --decorate
c7a04bc (HEAD, testing) add xx.txt
89f158d (master) a
9a00f20 a
6937c04 new file

切换到主线看看效果
[root@web01 data]# git checkout master
Switched to branch 'master'
[root@web01 data]# git branch
* master
testing
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa

 

把分支上的文件合并到主干上

git merge 分支名

[root@web01 data]# git branch
* master
  testing
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
[root@web01 data]# git branch
* master
  testing
[root@web01 data]# git checkout testing
Switched to branch 'testing'
[root@web01 data]# git branch
  master
* testing
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
-rw-r--r-- 1 root root 0 Feb 11 17:13 xx.txt
[root@web01 data]# git merge testing
Already up-to-date.

 

不行,也就是说merge的时候必须在master主干上

[root@web01 data]# git checkout master
Switched to branch 'master'
[root@web01 data]# git branch
* master
  testing
[root@web01 data]# git merge testing
Updating 89f158d..c7a04bc
Fast-forward
 xx.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 xx.txt
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
-rw-r--r-- 1 root root 0 Feb 11 17:19 xx.txt

 

分支同步完就可以删除了

[root@web01 data]# git branch -d testing
Deleted branch testing (was c7a04bc).
[root@web01 data]# git log --oneline --decorat
c7a04bc (HEAD, master) add xx.txt
89f158d a
9a00f20 a
6937c04 new file
[root@web01 data]# git log --oneline --decorate
c7a04bc (HEAD, master) add xx.txt
89f158d a
9a00f20 a
6937c04 new file
[root@web01 data]# ll
total 12
-rw-r--r-- 1 root root 3 Feb 11 15:30 a
-rw-r--r-- 1 root root 0 Feb 11 10:53 b
-rw-r--r-- 1 root root 0 Feb 11 10:53 c
-rw-r--r-- 1 root root 4 Feb 11 11:08 ha
-rw-r--r-- 1 root root 5 Feb 11 11:08 haa
-rw-r--r-- 1 root root 0 Feb 11 17:19 xx.txt
[root@web01 data]# git branch
* master

 

Git 冲突合并

当主干master和分支中有一样名称的文件,可内容不一样就会出现冲突,要解决这种冲突

在分支上操作
[root@web01 data]# git branch
  master
* testing
[root@web01 data]# cat a
baba
[root@web01 data]# echo "jin tian meichumeng" >>a
[root@web01 data]# cat a
baba
jin tian meichumeng
[root@web01 data]# git status
# On branch testing
# 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:   a
#
no changes added to commit (use "git add" and/or "git commit -a")
root@web01 data]# git commit -am "modified jin tian meichumeng"
[testing 02f3fcf] modified jin tian meichumeng
 1 file changed, 1 insertion(+)
[root@web01 data]# git status
# On branch testing
nothing to commit, working directory clean

在master上操作
[root@web01 data]# git checkout master
Switched to branch 'master'
[root@web01 data]# git branch
* master
  testing
[root@web01 data]# cat a
dadczcnzlcnzI I I

进行分支同步,发现冲突
[root@web01 data]# git merge testing
Auto-merging a
CONFLICT (content): Merge conflict in a
Automatic merge failed; fix conflicts and then commit the result.
[root@web01 data]# git status
# On branch master
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: a
#
no changes added to commit (use "git add" and/or "git commit -a")

查看冲突
[root@web01 data]# cat a
<<<<<<< HEAD
dadczcnzlcnzI I I
=======
baba
jin tian meichumeng
>>>>>>> testing

解决方法:

只能进行手动处理内容,删除特殊内容,选择保留什么

[root@web01 data]# vim a
dadczcnzlcnzI I I
baba
jin tian meichumeng

保存后再进行commit

[root@web01 data]# git commit -am "merge testing"
[master 8cadd36] merge testing
[root@web01 data]# git status
# On branch master
nothing to commit, working directory clean

注意的是分支里的原先a的内容和再master你手动改完的a内容不一样

Git拉去代码

拉去origin库的master主干代码

[root@web02 git_data]# git pull origin master
From 192.168.5.7:test/git_data
* branch master -> FETCH_HEAD
Already up-to-date.

Git推送代码到远程仓库

git push -u origin master

[root@web01 dzp]# git push -u origin master

 

Git批量删除文件

git add -A

git rm -rf --cached .

 

Git从别的远程仓库下载代码

git clone 远程仓库代码连接

 

Git远程仓库相关

git remote -v --------列出所有远程仓库

git remote remove 仓库名 ----------删除仓库

git remote add origin git@www.hebaobao.xx:dzp/dzp_data.git--添加远程仓库

git remote add origin git@www.hebaobao.xx:dzp/dzp_data.git

 

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年2月10日23:57:00
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Jenkins的Pipeline项目 Devops

Jenkins的Pipeline项目

Pipeline项目 CI/CD持续集成/持续部署 持续集成是一种软件开发实践,团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次,也就意味着每天可能发生多次集成。每次集成都通过自动化的构建...
jeesns项目代码部署and自动上线 Devops

jeesns项目代码部署and自动上线

jeesns项目代码自动上线 经过上一次失败的教训,这一次通过学习了maven项目管理工具,肯定能部署成功的。失败案例jeesns项目上线(失败案例),这对java项目的部署是一次很好的学习 Jenk...
maven工具,搭建私服nexus Devops

maven工具,搭建私服nexus

maven项目管理工具 Maven是一个项目管理的综合工具。Maven提供给开发人员构建一个完整的生命周期框架。 开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: