1.Devops介紹
1.Devops是什么
開發 development
運維 operations
2.Devops能干嘛
提高產品質量
1 自動化測試
2 持續集成
3 代碼質量管理工具
4 程序員鼓勵師
3.Devops如何實現
設計架構規划‐代碼的存儲‐構建‐測試、預生產、部署、監控
2.Git版本控制系統
1.版本控制系統簡介
vcs `version control system`
版本控制系統是一種記錄一個或若干個文件內容變化,以便將來查閱特定版本內容情況的系統
記錄文件的所有歷史變化
隨時可恢復到任何一個歷史狀態
多人協作開發
2.為什么需要版本控制系統
3.常見版本管理工具
SVN
集中式的版本控制系統,只有一個中央數據倉庫,如果中央數據倉庫掛了或者不可訪問,所有的使用者無法使用SVN,無
法進行提交或備份文件。
Git
3 .Git安裝
1. 系統環境准備
root@git‐git~]# cat /etc/redhat-release #查看系統版本
CentOS Linux release 7.1.1503 (Core)
[root@git‐git ~]# uname -r #查看內核版本
3.10.0‐229.el7.x86_64
[root@git‐git ~]# getenforce #確認Selinux關閉狀態
Disabled
[root@git‐git ~]# systemctl stop firewalld #關閉防火牆
2. Git安裝
[root@git‐git ~]# yum install git
3.Git部署配置
[root@git ~]# git config
‐‐global 使用全局配置文件
‐‐system 使用系統級配置文件
‐‐local 使用版本庫級配置文件
配置git使用用戶
[root@git‐git ~]# git config --global user.name "zeq"
配置git使用郵箱
[root@git‐git ~]# git config --global user.email "chn@eqnice.com"
語法高亮
[root@git‐git ~]# git config --global color.ui true
查看配置
[root@git‐git ~]# git config –‐list
user.name=zeq
user.email=chn@eqnice.com
color.ui=true
[root@git ~]# cat .gitconfig
[user]
name = zeq
email = chn@eqnice.com
[color]
ui = true
4.git初始化
- 初始化工作目錄、對已存在的目錄或者對已存在的目錄都可進行初始化
mkdir git_data
cd git_data/
- 初始化
git init
- 查看工作區狀態
git status
- 隱藏文件介紹:
branches # 分支目錄
config # 定義項目特有的配置選項
description # 僅供git web程序使用
HEAD # 指示當前的分支
hooks # 包含git鈎子文件
info # 包含一個全局排除文件(exclude文件)
objects # 存放所有數據內容,有info和pack兩個子文件夾
refs # 存放指向數據(分支)的提交對象的指針
index # 保存暫存區信息,在執行git init的時候,這個文件還沒有
4 .Git常規使用
1. 創建數據-提交數據
2. git四種狀態
3. git基礎命令
[root@git git_data]# git status
# 位於分支 master
# 初始提交
- 無文件要提交(創建/拷貝文件並使用 "git add" 建立跟蹤)
[root@git git_data]# touch a b c
[root@git git_data]# git status
# 位於分支 master
#
# 初始提交
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# a
# b
# c
提交為空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)
[root@git git_data]# git add a
[root@git git_data]# git status
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
# (使用 "git rm ‐‐cached <file>..." 撤出暫存區)
#
# 新文件: a
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# b
# c
[root@git git_data]# ll .git/
總用量 20
drwxr‐xr‐x 2 root root 6 8月 23 05:44 branches
‐rw‐r‐‐r‐‐ 1 root root 92 8月 23 05:44 config
‐rw‐r‐‐r‐‐ 1 root root 73 8月 23 05:44 description
‐rw‐r‐‐r‐‐ 1 root root 23 8月 23 05:44 HEAD
drwxr‐xr‐x 2 root root 4096 8月 23 05:44 hooks
‐rw‐r‐‐r‐‐ 1 root root 96 8月 23 07:06 index # git add a 把文件提交到了暫存區
drwxr‐xr‐x 2 root root 20 8月 23 05:44 info
drwxr‐xr‐x 5 root root 37 8月 23 07:06 objects
drwxr‐xr‐x 4 root root 29 8月 23 05:44 refs
[root@git git_data]# git add . # 使用git add . 或者* 添加目錄中所有改動過的文件
[root@git git_data]# git status
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
# (使用 "git rm ‐‐cached <file>..." 撤出暫存區)
#
# 新文件: a
# 新文件: b
# 新文件: c
[root@git git_data]# git rm ‐‐cached c
rm 'c'
[root@git git_data]# ll
總用量 0
‐rw‐r‐‐r‐‐ 1 root root 0 8月 23 07:05 a
‐rw‐r‐‐r‐‐ 1 root root 0 8月 23 07:05 b
‐rw‐r‐‐r‐‐ 1 root root 0 8月 23 07:05 c
[root@git git_data]# git status
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
# (使用 "git rm ‐‐cached <file>..." 撤出暫存區)
#
# 新文件: a
# 新文件: b
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# c
刪除文件
1.先從暫存區撤回到工作區、然后直接刪除文件
git rm ‐‐cached c
rm ‐f c
2.直接從暫存區域同工作區域一同刪除文件命令
git rm ‐f b
[root@git git_data]# git commit ‐m "commit a" # 提交到本地倉庫
[master(根提交) b4017a8] commit a
1 file changed, 0 insertions(+), 0 deletions(‐)
create mode 100644 a
[root@git git_data]# git status
# 位於分支 master
無文件要提交,干凈的工作區
- 修改文件名稱兩種方法
[root@git git_data]# mv a a.txt
[root@git git_data]# git status
# 位於分支 master
# 尚未暫存以備提交的變更:
# (使用 "git add/rm <file>..." 更新要提交的內容)
# (使用 "git checkout ‐‐ <file>..." 丟棄工作區的改動)
#
# 刪除: a
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# a.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit ‐a")
[root@git git_data]# git rm ‐‐cached a # 從暫存區刪除a文件
rm 'a'
[root@git git_data]# git status
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 刪除: a
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# a.txt
[root@git git_data]# git add a.txt
[root@git git_data]# git status
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 重命名: a ‐> a.txt # 識別到a和a.txt相同為重命名
[root@git git_data]# git commit ‐m "commit a.txt"
2.直接用git命令重命名
[root@git git_data]# git mv a.txt a 把工作區域和暫存區域的文件同時修改文件名稱
[root@git git_data]# git status
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 重命名: a.txt ‐> a
git commit ‐m "rename a.txt a"
git status 只能查看區域狀態的不同,不能查看文件內容的變化。
git diff 查看內容的不同
[root@git git_data]# echo aaa > a
[root@git git_data]# git diff a # 比對本地工作目錄和暫存區文件的不同
diff ‐‐git a/a b/a
index e69de29..72943a1 100644#
‐‐‐ a/a
+++ b/a
@@ ‐0,0 +1 @@
+aaa
[root@git git_data]# git add a # 提交a文件到暫存區域、在用git diff是相同的
[root@git git_data]# git diff ‐‐cached a # 比對的是暫存區和本地倉庫文件的不同處
diff ‐‐git a/a b/a
index e69de29..72943a1 100644
‐‐‐ a/a
+++ b/a
@@ ‐0,0 +1 @@
+aaa
[root@git git_data]# git commit ‐m "modified a" # 提交后在比對則暫存區和本地倉庫內容相同
[master 4c57a60] modified a
1 file changed, 1 insertion(+)
[root@git git_data]# git diff ‐‐cached a
[root@git git_data]#
git commit # 相當於虛擬機的鏡像、任何操作都被做了一次快照,可恢復到任意一個位置
[root@git git_data]# git log 查看歷史的git commit快照操作
commit 4c57a605997f511149bfec53d9018b503e77f961 # 哈希唯一標識的字符串
Author: lizhenya <lizhenya@qq.com> # 作者個人信息
Date: Thu Aug 23 07:54:23 2018 +0800 # 時間
modified a # ‐m 個人寫的提交描述信息
commit 56925321114eb9adf09b42a733a6f9f3edd9ad65
Author: lizhenya <lizhenya@qq.com>
Date: Thu Aug 23 07:39:41 2018 +0800
rename a.txt a
commit 7adfca06559ef7739dffdc11ecb7fb8800a9931a
Author: lizhenya <lizhenya@qq.com>
Date: Thu Aug 23 07:36:47 2018 +0800
commit a.txt
commit b4017a876cfed78425fe58e7ecbcd49199ed5a11
Author: lizhenya <lizhenya@qq.com>
Date: Thu Aug 23 07:22:29 2018 +0800
[root@git git_data]# git log ‐‐oneline # 一行簡單的顯示commit信息
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
[root@git git_data]# git log ‐‐oneline ‐‐decorate # 顯示當前的指針指向哪里
4c57a60 (HEAD, master) modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
[root@git git_data]# git log ‐p # 顯示具體內容的變化
[root@git git_data]# git log ‐1 # 只顯示1條內容
恢復歷史數據
1.只更改了當前目錄
[root@git git_data]# echo "333" >> a
[root@git git_data]# git status
# 位於分支 master
# 尚未暫存以備提交的變更:
# (使用 "git add <file>..." 更新要提交的內容)
# (使用 "git checkout ‐‐ <file>..." 丟棄工作區的改動) # 看提示使用此命令覆蓋工作區的改動
#
# 修改: a
#
修改尚未加入提交(使用 "git add" 和/或 "git commit ‐a")
[root@git git_data]# git checkout ‐‐ a # 從暫存區覆蓋本地工作目錄
[root@git git_data]# git status
# 位於分支 master
無文件要提交,干凈的工作區
[root@git git_data]# cat a
aaa
2.修改了本地目錄且同時提交到了暫存區
[root@git git_data]# echo ccc >> a # 添加新內容
[root@git git_data]# git add . # 提交到暫存區
[root@git git_data]# git diff ‐‐cached #比對暫存區和本地倉庫的內容
diff ‐‐git a/a b/a
index 72943a1..959479a 100644
‐‐‐ a/a
+++ b/a
@@ ‐1 +1,2 @@
aaa
+ccc
[root@git git_data]# git status
# 位於分支 master
# 要提交的變更:
# (使用 "git reset HEAD <file>..." 撤出暫存區)
#
# 修改: a
[root@git git_data]# git reset HEAD a # 本地倉庫覆蓋暫存區域
重置后撤出暫存區的變更:
M a
[root@git git_data]# git diff a
diff ‐‐git a/a b/a
index 72943a1..959479a 100644
‐‐‐ a/a
+++ b/a
@@ ‐1 +1,2 @@
aaa
+ccc
[root@git git_data]# git diff ‐‐cached a
[root@git git_data]#
3.修改了工作目錄后提交到了暫存區和本地倉庫后進行數據恢復
echo bbb >>a # 提交新的bbb文件到a
git commit ‐m "add bbb"
echo ccc >> a
git commit ‐am "add ccc" # 這時候發現改錯代碼了,想還原某一次提交的文件快照
[root@git git_data]# git log ‐‐oneline
59ba2a9 add ccc
dbead4c add bbb
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
`Git服務程序中有一個叫做HEAD的版本指針,當用戶申請還原數據時,其實就是將HEAD指針指向到某個特定的提交
版本,但是因為Git是分布式 版本控制系統,為了避免歷史記錄沖突,故使用了SHA‐1計算出十六進制的哈希字串
來區分每個提交版本,另外默認的HEAD版本指針會指向到最近的一次提交版本記錄`
[root@git git_data]# git reset ‐‐hard 4c57a60
HEAD 現在位於 4c57a60 modified a
`剛剛的操作實際上就是改變了一下HEAD版本指針的位置,就是你將HEAD指針放在那里,那么你的當前工作版本就
會定位在那里,要想把內容再還原到最新提交的版本,先看查看下提交版本號`
[root@git git_data]# cat a # 打開發現回退錯了,應該回退到bbb版本
aaa
[root@git git_data]# git log ‐‐oneline # 這時候查看log沒有commit bbb的歷史了
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
`怎么搞得?竟然沒有了add bbb這個提交版本記錄?
原因很簡單,因為我們當前的工作版本是歷史的一個提交點,這個歷史提交點還沒有發生過add bbb 更新記錄,所
以當然就看不到了,要是想”還原到未來”的歷史更新點,可以用git reflog命令來查看所有的歷史記錄:`
[root@git git_data]# git reflog # 使用git reflog 可查看總歷史內容
4c57a60 HEAD@{0}: reset: moving to 4c57a60
59ba2a9 HEAD@{1}: commit: add ccc
dbead4c HEAD@{2}: commit: add bbb
4c57a60 HEAD@{3}: commit: modified a
5692532 HEAD@{4}: commit: rename a.txt a
7adfca0 HEAD@{5}: commit: commit a.txt
b4017a8 HEAD@{6}: commit (initial): commit a
[root@git git_data]# git reset ‐‐hard dbead4c # 然后使用reset回到bbb的版本內容下
HEAD 現在位於 dbead4c add bbb
[root@git git_data]# cat a
aaa
bbb
5. git分支
分支即是平行空間,假設你在為某個手機系統研發拍照功能,代碼已經完成了80%,但如果將這不完整的代碼直接
提交到git倉庫中,又有可能影響到其他人的工作,此時我們便可以在該軟件的項目之上創建一個名叫”拍照功
能”的分支,這種分支只會屬於你自己,而其他人看不到,等代碼編寫完成后再與原來的項目主分支合並下即可,這
樣即能保證代碼不丟失,又不影響其他人的工作。
一般在實際的項目開發中,我們要盡量保證master分支是非常穩定的,僅用於發布新版本,平時不要隨便直接修
改里面的數據文件,而工作的時候則可以新建不同的工作分支,等到工作完成后在合並到master分支上面,所以團
隊的合作分支看起來會像上面圖那樣。
[root@git git_data]# git log ‐‐oneline ‐‐decorate
dbead4c (HEAD, master) add bbb # 默認分支指向你最后一次的提交 HEAD頭、指針
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
HEAD 指針指向哪個分支、說明你當前在哪個分支下工作`
[root@git git_data]# git branch testing # 新建testing分支
[root@git git_data]# git branch
* master # *號在哪里就說明當前在哪個分支上入下圖所示
testing
[root@git git_data]# git log ‐‐oneline ‐‐decorate # 通過命令查看分支指向
dbead4c (HEAD, testing, master) add bbb
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
[root@git git_data]# git checkout testing # 切換到testing分支、對應的HEAD指針也指向了testing
切換到分支 'testing'
[root@git git_data]# git branch
master
* testing
[root@git git_data]# touch test
[root@git git_data]# git add .
[root@git git_data]# git commit ‐m "commit test"
[root@git git_data]# git checkout master # 切換到master分支后指針指向到了master
切換到分支 'master'
[root@git git_data]# git branch
* master
testing
[root@git git_data]# ll # 正常情況下是沒有test文件的、保證master分支是線上環境的
總用量 4
‐rw‐r‐‐r‐‐ 1 root root 8 8月 23 08:42 a
[root@git git_data]# touch master
[root@git git_data]# git add .
[root@git git_data]# git commit ‐m "commit master"
合並分支
[root@git git_data]# git merge testing # 提示輸入描述信息 相當於git的‐m參數
[root@git git_data]# git log ‐‐oneline ‐‐decorate
3258705 (HEAD, master) Merge branch 'testing'
f5ae1d8 commit master
ad4f25a (testing) commit test
dbead4c add bbb
4c57a60 modified a
5692532 rename a.txt a
7adfca0 commit a.txt
b4017a8 commit a
沖突合並
[root@git git_data]# echo "master" >> a
[root@git git_data]# git commit ‐am "modified a master"
[root@git git_data]# git checkout testing
切換到分支 'testing'
[root@git git_data]# git branch
master
* testing
[root@git git_data]# cat a
aaa
bbb
[root@git git_data]# echo "testing" >>a
[root@git git_data]# git commit ‐am "modified a on testing branch"
[root@git git_data]# git checkout master
[root@git git_data]# git merge testing
自動合並 a
沖突(內容):合並沖突於 a
自動合並失敗,修正沖突然后提交修正的結果。
[root@git git_data]# cat a # 沖突的文件自動標識到文件里,手動更改沖突要保留的代碼
[root@git git_data]# git commit ‐am "merge testing to master" # 進行提交即可
[root@git git_data]# git log ‐‐oneline ‐‐decorate
bba413d (HEAD, master) merge testing to master
34d7a55 (testing) modified a on testing branch
ec1a424 modified a master
3258705 Merge branch 'testing'
f5ae1d8 commit master
ad4f25a commit test
刪除分支‐d參數
[root@git git_data]# git branch ‐d testing
已刪除分支 testing(曾為 34d7a55)。
[root@git git_data]# git branch
* master
6.git標簽使用
標簽也是指向了一次commit提交,是一個里程碑式的標簽,回滾打標簽直接加標簽號,不需要加唯一字符串不好記
[root@git git_data]# git tag ‐a v1.0 ‐m "aaa bbb master tesing version v1.0" # ‐a指定標簽名字 ‐m 指定說明文字
[root@git git_data]# git tag
v1.0
[root@git git_data]# git tag ‐a v2.0 dbead4c ‐m "add bbb version v2.0" # 指定某一次的提交為標簽
[root@git git_data]# git show v1.0 # 查看v1.0的信息 git show 加標簽查看
[root@git git_data]# git reset ‐‐hard v2.0 # 直接還原數據到v2.0
HEAD 現在位於 dbead4c add bbb
[root@git git_data]# ll
總用量 4
‐rw‐r‐‐r‐‐ 1 root root 8 8月 23 11:26 a
‐rw‐r‐‐r‐‐ 1 root root 0 8月 23 11:25 b
[root@git git_data]# git tag ‐d v2.0 # 刪除標簽 ‐d參數
5. github使用
1.什么是github
Github顧名思義是一個Git版本庫的托管服務,是目前全球最大的軟件倉庫,擁有上百萬的開發者用戶,也是軟件
開發和尋找資源的最佳途徑,Github不僅可以托管各種Git版本倉庫,還擁有了更美觀的Web界面,您的代碼文件可
以被任何人克隆,使得開發者為開源項貢獻代碼變得更加容易,當然也可以付費購買私有庫,這樣高性價比的私有
庫真的是幫助到了很多團隊和企業
2.github操作
1、注冊用戶
2、配置ssh‐key
3、創建項目
4、克隆項目到本地
5、推送新代碼到github
1.注冊用戶
訪問github網站
https://github.com/
點擊注冊
注冊完成后添加新的項目
添加新項目之前要驗證郵箱
2.配置ssh‐key
添加一個密鑰
git服務器創建秘鑰
[root@git ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:n/V2kCiwwm2UfBsnQLm17eXUCBiBByyPbefmz5oQvfU root@gitlab
The key's randomart image is:
+---[RSA 2048]----+
| o++o+ |
| ..+o+ . |
| ==++o.. o |
| ..o==o=..+..|
| o.So+.++o |
| o oo*.o.. |
| .o+ E .|
| ..o . . |
| ooo |
+----[SHA256]-----+
[root@git ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmv4aEEEpbUyzv1r6SN0JqOfeyQ7sZZbXxWFv4xflIJeK/rl8cF7UYCzjLEvwJlrkIjKSs5uW1x0zWEcZFiv5tGCiO7DeMR6pKUAn7NzNjKiCcElCXiqHVew84iTbxX4MWKlbFoJYO9/wQ1NlrQfqcSgZwJTLKBMVoMXvTWPPGXf6AwdSp68guFwwGDIV8BiHZiy61bKiWYSVKSDP47Y7VUV/bdwGaxG7tAfalWVpe6xXXRtsj58sENyIWbRI7/9XWqs+eV+CgI74YjOanMvHnHFlfg0tb+MewRb4tFGVmroFBRsvfI3Sl2fez2zHG0qh3f34/0KF1kitlWkgcBJqN root@git
3.創建項目
4.克隆項目到本地
http方式克隆
[root@git ~]# git clone https://github.com/eqzhang/zeq.git
正克隆到 'zeq'...
warning: 您似乎克隆了一個空版本庫。
[root@git ~]# ll
總用量 4
-rw-------. 1 root root 1271 9月 3 17:37 anaconda-ks.cfg
drwxr-xr-x 3 root root 18 11月 20 19:47 zeq
ssh方式克隆
[root@git ~]# git clone git@github.com:eqzhang/zeq.git
正克隆到 'zeq'...
warning: 您似乎克隆了一個空版本庫。
[root@git ~]# ll
總用量 4
-rw-------. 1 root root 1271 9月 3 17:37 anaconda-ks.cfg
drwxr-xr-x 3 root root 18 11月 20 19:51 zeq
如果用其中一種方式克隆成功以后,在用另一種方式克隆的話會報錯(版本庫已經存在),這時候可以刪除存在的倉庫在執行克隆
5.推送新代碼到github
創建一個index.html的文件推送到github
[root@git ~]# cd zeq/
[root@git zeq]# echo 123 > index.html
[root@git zeq]# ll
總用量 4
-rw-r--r-- 1 root root 4 11月 20 19:54 index.html
[root@git zeq]# git add .
[root@git zeq]# git push -u origin master #git push 推送本地倉庫代碼到遠程倉庫
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/eqzhang/zeq/pull/new/master
remote:
To git@github.com:eqzhang/zeq.git
* [new branch] master -> master
分支 master 設置為跟蹤來自 origin 的遠程分支 master。
進入github網頁查看
[root@git zeq]# git pull origin # 拉取遠程倉庫最新代碼、然后進行上傳
- 上面操作是本地沒有倉庫,克隆的遠程倉庫
- 如果我本地有本地倉庫,我想把本地倉庫的代碼上傳到我的遠程倉庫則需要進行關聯遠程倉庫
[root@git zeq]# git remote add origin git@github.com:eqzhang/zeq.git
[root@git zeq]# git remote
origin
[root@git zeq]# git push -u origin master
- 如果已存在origin則用rm刪除
[root@git zeq]# git remote add origin git@github.com:eqzhang/zeq.git
fatal: 遠程 origin 已經存在。
[root@git zeq]# git remote rm origin
[root@git zeq]# git remote
[root@git zeq]#