git-jenkins階段01 DevOps介紹, 版本控制系統, Git的安裝與應用(內容對比,歷史記錄,恢復,分支)

战斗小人發表於2024-06-09

1.DevOps介紹

鐵三角
開發    測試    運維

老闆的想法  產品經理的構造  開發的程式碼實現  測試的功能測試  運維平臺構建  程式碼的上線

開發    測試        變化    程式碼的更新
運維             穩定      網站能夠正常執行下去

2. 版本控制系統

vcs
記錄檔案的所有的歷史變化
隨時可以恢復到任何一個歷史狀態
多人進行協作開發

常見的版本管理工具
Git
SVN        集中式的版本控制(SVN公司管理)

3.Git的安裝與應用

#環境準備(需要2g記憶體)
[root@git ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

[root@git ~]# uname -r
3.10.0-957.el7.x86_64

[root@git ~]# getenforce
Disabled

[root@git ~]# iptables-save        #命令執行沒有輸出結果,表示防火牆是關閉的
[root@git ~]# 

#安裝
[root@git ~]# 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    #使用版本庫級別配置

#配置git使用使用者
[root@git ~]# git config --global user.name "qls"
[root@git ~]# git config --global user.email "123@qq.com"    #配置使用者的郵箱
#配置全域性,語法高亮
[root@git ~]# git config --global color.ui true
#顯示配置列表
[root@git ~]# git config --list
user.name=qls
user.email=123@qq.com
color.ui=true
#此時會多出.gitconfig, 就是git的配置檔案
[root@git ~]# ll -a
total 40
...
-rw-r--r--   1 root root   58 Jun  4 00:34 .gitconfig
#檢視配置檔案
[root@git ~]# cat .gitconfig 
[user]
    name = qls
    email = 123@qq.com
[color]
    ui = true

#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 Jun  4 00:40 branches        #分支目錄
-rw-r--r-- 1 root root  92 Jun  4 00:40 config            #特有的配置選項
-rw-r--r-- 1 root root  73 Jun  4 00:40 description        #Git web程式使用的
-rw-r--r-- 1 root root  23 Jun  4 00:40 HEAD            #指示當前的分支
drwxr-xr-x 2 root root 242 Jun  4 00:40 hooks            #Git的鉤子檔案
drwxr-xr-x 2 root root  21 Jun  4 00:40 info            #全域性排除檔案(exclude檔案)
drwxr-xr-x 4 root root  30 Jun  4 00:40 objects            #存放所有資料     info  pack
drwxr-xr-x 4 root root  31 Jun  4 00:40 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的基礎命令

[root@git git_data]# git status        #顯示當前工作區的狀態
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

#建立一些測試檔案
[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 status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   oldboy.txt
#    new file:   oldgirl.txt
#    new file:   test.txt

[root@git git_data]# git rm --cached test.txt    #從暫存區將檔案刪除
rm '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:   oldboy.txt
#    new file:   oldgirl.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    test.txt
[root@git git_data]# rm -rf 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:   oldboy.txt
#    new file:   oldgirl.txt


#刪除檔案
兩種方法

第一種

1.先刪除暫存區裡面的檔案,再刪除工作目錄中的檔案
git rm --cached test.txt
rm -rf test.txt

2.直接從暫存區連同工作目錄中的檔案刪除
[root@git git_data]# git rm -f oldgirl.txt
rm 'oldgirl.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:   oldboy.txt
#
[root@git git_data]# ls
oldboy.txt

#從暫存區提交到本地倉庫    -m 註釋資訊
[root@git git_data]# git commit -m "new 3 file"
[master (root-commit) 220403a] new 3 file
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
 create mode 100644 b.txt
 create mode 100644 oldboy.txt
[root@git git_data]# git status
# On branch master
nothing to commit, working directory clean

#檔案重新命名
兩種方法
1.本地重新命名,修改工作目錄中檔案的名稱
[root@git git_data]# mv a.txt aaa.txt
[root@git 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)
#
#    aaa.txt
no changes added to commit (use "git add" and/or "git commit -a")

2.刪除暫存區中的檔案
[root@git git_data]# git rm --cached a.txt
rm 'a.txt'
[root@git git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    deleted:    a.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    aaa.txt

3.將重新命名好的檔案提交到暫存區
[root@git git_data]# git add .
[root@git git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    a.txt -> aaa.txt

4.提交到本地倉庫
[root@git git_data]# git commit -m "mv a.txt aaa.txt"
[master 1c67b18] mv a.txt aaa.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a.txt => aaa.txt (100%)

第二種方法

直接使用git進行重新命名
[root@git git_data]# git mv b.txt bbb.txt
[root@git git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    renamed:    b.txt -> bbb.txt
#

提交到本地倉庫
[root@git git_data]# git commit -m "mv b.txt bbb.txt"
[master 87498ce] mv b.txt bbb.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename b.txt => bbb.txt (100%)
 
 #檔案內容比對
 
 #比對工作目錄與暫存區檔案內容不同之處
 [root@git git_data]# echo "1111111111111" >> oldboy.txt
 [root@git git_data]# git diff oldboy.txt
diff --git a/oldboy.txt b/oldboy.txt
index e69de29..a5fdf3a 100644
--- a/oldboy.txt
+++ b/oldboy.txt
@@ -0,0 +1 @@
+1111111111111

[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
diff --git a/oldboy.txt b/oldboy.txt
index e69de29..a5fdf3a 100644
--- a/oldboy.txt
+++ b/oldboy.txt
@@ -0,0 +1 @@
+1111111111111
[root@git git_data]# git commit -m "modify oldboy.txt"    #提交到本地倉庫
[master 91d1614] modify oldboy.txt
 1 file changed, 1 insertion(+)
[root@git git_data]# git diff --cached oldboy.txt


[root@git git_data]# echo "222222" >> oldboy.txt 
#同時提交暫存區和本地倉庫
[root@git git_data]# git commit -am "modify oldboy.txt 2"
[master a88a12f] modify oldboy.txt 2
 1 file changed, 1 insertion(+)
 
#檢視Git的歷史操作記錄
[root@git git_data]# git log
commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524
Author: qls <123@qq.com>
Date:   Fri Jun 7 04:34:28 2024 +0800

    modify oldboy.txt 2

#使用一行來顯示Git的歷史記錄
[root@git git_data]# git log --oneline
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file

#decorate 顯示當前指標所指向的分支
[root@git git_data]# git log --oneline --decorate
a88a12f (HEAD, master) modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file

#顯示具體內容的變化
[root@git git_data]# git log -p
commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524
Author: qls <123@qq.com>
Date:   Fri Jun 7 04:34:28 2024 +0800

    modify oldboy.txt 2

diff --git a/oldboy.txt b/oldboy.txt
index a5fdf3a..c217021 100644
--- a/oldboy.txt
+++ b/oldboy.txt
@@ -1 +1,2 @@
 1111111111111
+222222
...

#顯示最近的一次記錄(可修改次數)
[root@git git_data]# git log -1
commit a88a12fc0ef8e6a525c9a8e137a246f5f8459524
Author: qls <123@qq.com>
Date:   Fri Jun 7 04:34:28 2024 +0800

    modify oldboy.txt 2

#恢復歷史資料
1.改變了工作區中檔案的內容,發現改錯了(或者檔案被誤刪了)
[root@git git_data]# echo "44444" >> oldboy.txt
[root@git git_data]# git status
# On branch master
# 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:   oldboy.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
#將暫存區裡面的內容覆蓋到工作目錄(被誤刪後也可以拉回來)
[root@git git_data]# git checkout -- oldboy.txt
[root@git git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@git git_data]# cat oldboy.txt 
1111111111111
222222

2.工作目錄和暫存區都發生了改變,沒有提交到本地倉庫,發現改錯了
[root@git git_data]# echo "444444" >>oldboy.txt 
[root@git git_data]# git add .
[root@git git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)    #將本地倉庫恢復到暫存區
#
#    modified:   oldboy.txt
#
#比對本地倉庫和暫存區的不同
[root@git git_data]# git diff --cached oldboy.txt
diff --git a/oldboy.txt b/oldboy.txt
index c217021..54bb024 100644
--- a/oldboy.txt
+++ b/oldboy.txt
@@ -1,2 +1,3 @@
 1111111111111
 222222
+444444

#從本地倉庫覆蓋到暫存區
[root@git git_data]# git reset HEAD oldboy.txt
Unstaged changes after reset:
M    oldboy.txt
[root@git git_data]# git diff --cached oldboy.txt
[root@git git_data]# cat oldboy.txt 
1111111111111
222222
444444

#從暫存區覆蓋工作目錄
[root@git git_data]# git checkout -- oldboy.txt
[root@git git_data]# cat oldboy.txt 
1111111111111
222222

3.修改了工作區,暫存區,也提交到了本地倉庫,發現寫錯了
[root@git git_data]# echo "44444" >> oldboy.txt 
[root@git git_data]# git add .
[root@git git_data]# git commit -m "modify oldboy.txt 4"
[master 09308a3] modify oldboy.txt 4
 1 file changed, 1 insertion(+)
[root@git git_data]# git status
# On branch master
nothing to commit, working directory clean

#透過歷史記錄來恢復資料

[root@git git_data]# git log --oneline
09308a3 modify oldboy.txt 4
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file
[root@git git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@git git_data]# git reset --hard a88a12f    #將所有地方的資料恢復到這個快照
HEAD is now at a88a12f modify oldboy.txt 2
[root@git git_data]# cat oldboy.txt 
1111111111111
222222
[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那一次的修改了
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file
[root@git git_data]# git reflog        #顯示所有的git歷史記錄
a88a12f HEAD@{0}: reset: moving to a88a12f
09308a3 HEAD@{1}: commit: modify oldboy.txt 4
a88a12f HEAD@{2}: commit: modify oldboy.txt 2
91d1614 HEAD@{3}: commit: modify oldboy.txt
87498ce HEAD@{4}: commit: mv b.txt bbb.txt
1c67b18 HEAD@{5}: commit: mv a.txt aaa.txt
220403a HEAD@{6}: commit (initial): new 3 file
[root@git git_data]# git reset --hard 09308a3
HEAD is now at 09308a3 modify oldboy.txt 4
[root@git git_data]# cat oldboy.txt 
1111111111111
222222
44444

#git分支

#顯示你當前的指標指向哪個分支
[root@git git_data]# git log --oneline --decorate
09308a3 (HEAD, master) modify oldboy.txt 4
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a 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
Switched to branch 'test'
[root@git git_data]# git branch
  master
* test

[root@git git_data]# touch test.txt
[root@git git_data]# git add .
[root@git git_data]# git commit -m "test commit"
[test d4c9af7] test commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@git git_data]# git log --oneline --decorate
d4c9af7 (HEAD, test) test commit
09308a3 (master) modify oldboy.txt 4
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file
[root@git git_data]# git checkout master    #切換到master分支
Switched to branch 'master'
[root@git git_data]# ll
total 4
-rw-r--r-- 1 root root  0 Jun  7 02:57 aaa.txt
-rw-r--r-- 1 root root  0 Jun  7 02:57 bbb.txt
-rw-r--r-- 1 root root 27 Jun  7 08:14 oldboy.txt
[root@git git_data]# git branch
* master
  test

#合併分支
[root@git git_data]# git merge test
Updating 09308a3..d4c9af7
Fast-forward
 test.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@git git_data]# ll        #合併分支之後,test建立的檔案就顯示出來了
total 4
-rw-r--r-- 1 root root  0 Jun  7 02:57 aaa.txt
-rw-r--r-- 1 root root  0 Jun  7 02:57 bbb.txt
-rw-r--r-- 1 root root 27 Jun  7 08:14 oldboy.txt
-rw-r--r-- 1 root root  0 Jun  7 21:40 test.txt
[root@git git_data]# git log --oneline --decorate
d4c9af7 (HEAD, test, master) test commit
09308a3 modify oldboy.txt 4
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file

#子分支上,合併主分支內容
[root@git git_data]# touch master.txt
[root@git git_data]# ll
total 4
-rw-r--r-- 1 root root  0 Jun  7 02:57 aaa.txt
-rw-r--r-- 1 root root  0 Jun  7 02:57 bbb.txt
-rw-r--r-- 1 root root  0 Jun  7 21:44 master.txt
-rw-r--r-- 1 root root 27 Jun  7 08:14 oldboy.txt
-rw-r--r-- 1 root root  0 Jun  7 21:40 test.txt
[root@git git_data]# git add .
[root@git git_data]# git commit -m "master touch master.txt"
[master b912bb0] master touch master.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.txt
[root@git git_data]# git checkout test
Switched to branch 'test'
[root@git git_data]# ll
total 4
-rw-r--r-- 1 root root  0 Jun  7 02:57 aaa.txt
-rw-r--r-- 1 root root  0 Jun  7 02:57 bbb.txt
-rw-r--r-- 1 root root 27 Jun  7 08:14 oldboy.txt
-rw-r--r-- 1 root root  0 Jun  7 21:40 test.txt
[root@git git_data]# git merge master    #合併主分支
Updating d4c9af7..b912bb0
Fast-forward
 master.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.txt
[root@git git_data]# ll
total 4
-rw-r--r-- 1 root root  0 Jun  7 02:57 aaa.txt
-rw-r--r-- 1 root root  0 Jun  7 02:57 bbb.txt
-rw-r--r-- 1 root root  0 Jun  7 21:45 master.txt
-rw-r--r-- 1 root root 27 Jun  7 08:14 oldboy.txt
-rw-r--r-- 1 root root  0 Jun  7 21:40 test.txt

#合併衝突
[root@git git_data]# git checkout master
Switched to branch 'master'
[root@git git_data]# git branch
* master
  test
[root@git git_data]# echo "aaaa" >>oldboy.txt 
[root@git git_data]# git commit -am "modify master"
[master b5bf6fd] modify master
 1 file changed, 1 insertion(+)

[root@git git_data]# git checkout test
Switched to branch 'test'
[root@git git_data]# echo "bbbb" >>oldboy.txt 
[root@git git_data]# git commit -am "modify test"
[test c22a40f] modify test
 1 file changed, 1 insertion(+)
 
 [root@git git_data]# git checkout master
Switched to branch 'master'
[root@git git_data]# git merge test        #合併發生衝突
Auto-merging oldboy.txt
CONFLICT (content): Merge conflict in oldboy.txt
Automatic merge failed; fix conflicts and then commit the result.

[root@git git_data]# cat oldboy.txt 
1111111111111
222222
44444
<<<<<<< HEAD    #當前分支操作
aaaa
=======
bbbb            #test指標的操作
>>>>>>> test
[root@git git_data]# git log --oneline --decorate
b5bf6fd (HEAD, master) modify master
b912bb0 master touch master.txt
d4c9af7 test commit
09308a3 modify oldboy.txt 4
a88a12f modify oldboy.txt 2
91d1614 modify oldboy.txt
87498ce mv b.txt bbb.txt
1c67b18 mv a.txt aaa.txt
220403a new 3 file

#手動修改衝突
[root@git git_data]# vim oldboy.txt
1111111111111
222222
44444
aaaa
[root@git git_data]# git commit -am "commit modify oldboy.txt"
[master 3f8051e] commit modify oldboy.txt

#test分支 合併分支
[root@git git_data]# git checkout test
Switched to branch 'test'
[root@git git_data]# cat oldboy.txt 
1111111111111
222222
44444
bbbb
[root@git git_data]# git merge master
Updating c22a40f..3f8051e
Fast-forward
 oldboy.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@git git_data]# cat oldboy.txt 
1111111111111
222222
44444
aaaa

#刪除分支
[root@git git_data]# git checkout master
Switched to branch 'master'
[root@git git_data]# git branch -d test
Deleted branch test (was 3f8051e).
[root@git git_data]# git branch
* master

[root@git git_data]# git reflog
3f8051e HEAD@{0}: checkout: moving from test to master
3f8051e HEAD@{1}: merge master: Fast-forward
c22a40f HEAD@{2}: checkout: moving from master to test
3f8051e HEAD@{3}: commit (merge): commit modify oldboy.txt
b5bf6fd HEAD@{4}: checkout: moving from test to master
c22a40f HEAD@{5}: commit: modify test
b912bb0 HEAD@{6}: checkout: moving from master to test
b5bf6fd HEAD@{7}: reset: moving to b5bf6fd
f4e3d14 HEAD@{8}: commit: modify test    #z這裡改錯了,在主分支修改了,做了一次回退
b5bf6fd HEAD@{9}: commit: modify master
b912bb0 HEAD@{10}: checkout: moving from test to master
b912bb0 HEAD@{11}: merge master: Fast-forward
d4c9af7 HEAD@{12}: checkout: moving from master to test
b912bb0 HEAD@{13}: commit: master touch master.txt
d4c9af7 HEAD@{14}: merge test: Fast-forward
09308a3 HEAD@{15}: checkout: moving from test to master
d4c9af7 HEAD@{16}: commit: test commit
09308a3 HEAD@{17}: checkout: moving from master to test
09308a3 HEAD@{18}: reset: moving to 09308a3
a88a12f HEAD@{19}: reset: moving to a88a12f
09308a3 HEAD@{20}: commit: modify oldboy.txt 4
a88a12f HEAD@{21}: commit: modify oldboy.txt 2
91d1614 HEAD@{22}: commit: modify oldboy.txt
87498ce HEAD@{23}: commit: mv b.txt bbb.txt
1c67b18 HEAD@{24}: commit: mv a.txt aaa.txt
220403a HEAD@{25}: commit (initial): new 3 file

相關文章