重新新增一個Markdown格式的git使用
1.Devops是什麼
開發 development
運維 operations
2.Devops能幹嘛
提高產品質量 1 自動化測試 2 持續整合 3 程式碼質量管理工具 4 程式員鼓勵師
3.Devops如何實現
設計架構規劃‐程式碼的儲存‐構建‐測試、預生產、部署、監控
2.Git版本控制系統
1.版本控制系統簡介
vcs`version control system` 版本控制系統是一種記錄一個或若干個檔案內容變化,以便將來查閱特定版本內容情況的系統 記錄檔案的所有歷史變化 隨時可恢復到任何一個歷史狀態 多人協作開發
2.為什麼需要版本控制系統
3.常見版本管理工具
集中式的版本控制系統,只有一箇中央資料倉庫,如果中央資料倉庫掛了或者不可訪問,所有的使用者無法使用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 "[email protected]"
語法高亮
[root@git‐git ~]# git config --global color.ui true
檢視配置
[root@git‐git ~]# git config –‐list user.name=zeq [email protected] color.ui=true [root@git ~]# cat .gitconfig [user] name = zeq email = [email protected] [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 root6 8月23 05:44 branches ‐rw‐r‐‐r‐‐ 1 root root92 8月23 05:44 config ‐rw‐r‐‐r‐‐ 1 root root73 8月23 05:44 description ‐rw‐r‐‐r‐‐ 1 root root23 8月23 05:44 HEAD drwxr‐xr‐x 2 root root 4096 8月23 05:44 hooks ‐rw‐r‐‐r‐‐ 1 root root96 8月23 07:06 index# git add a 把檔案提交到了暫存區 drwxr‐xr‐x 2 root root20 8月23 05:44 info drwxr‐xr‐x 5 root root37 8月23 07:06 objects drwxr‐xr‐x 4 root root29 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 <[email protected]># 作者個人資訊 Date:Thu Aug 23 07:54:23 2018 +0800# 時間 modified a# ‐m 個人寫的提交描述資訊 commit 56925321114eb9adf09b42a733a6f9f3edd9ad65 Author: lizhenya <[email protected]> Date:Thu Aug 23 07:39:41 2018 +0800 rename a.txt a commit 7adfca06559ef7739dffdc11ecb7fb8800a9931a Author: lizhenya <[email protected]> Date:Thu Aug 23 07:36:47 2018 +0800 commit a.txt commit b4017a876cfed78425fe58e7ecbcd49199ed5a11 Author: lizhenya <[email protected]> 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# 本地倉庫覆蓋暫存區域 重置後撤出暫存區的變更: Ma [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網站
ofollow,noindex" target="_blank">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-x3 root root18 11月 20 19:47 zeq
ssh方式克隆 [root@git ~]# git clone [email protected]:eqzhang/zeq.git 正克隆到 'zeq'... warning: 您似乎克隆了一個空版本庫。 [root@git ~]# ll 總用量 4 -rw-------. 1 root root 1271 9月3 17:37 anaconda-ks.cfg drwxr-xr-x3 root root18 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 [email protected]:eqzhang/zeq.git * [new branch]master -> master 分支 master 設定為跟蹤來自 origin 的遠端分支 master。
進入github網頁檢視
[root@git zeq]# git pull origin# 拉取遠端倉庫最新程式碼、然後進行上傳
- 上面操作是本地沒有倉庫,克隆的遠端倉庫
- 如果我本地有本地倉庫,我想把本地倉庫的程式碼上傳到我的遠端倉庫則需要進行關聯遠端倉庫
[root@git zeq]# git remote add origin [email protected]: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 [email protected]:eqzhang/zeq.git fatal: 遠端 origin 已經存在。 [root@git zeq]# git remote rm origin [root@git zeq]# git remote [root@git zeq]#