Git介紹及常用操作演示(一)--技術流ken
Git介紹及常用操作演示(一)--技術流ken
Git介紹
Git(讀音為/gɪt/。)是一個開源的分散式版本控制系統,可以有效、高速的處理從很小到非常大的專案版本管理。 Git 是 Linus Torvalds 為了幫助管理 Linux 核心開發而開發的一個開放原始碼的版本控制軟體。--摘自360百科
Git是分散式版本控制系統,那麼它就沒有中央伺服器的,每個人的電腦就是一個完整的版本庫,所以,工作的時候就不需要聯網了,因為版本庫都是在自己的電腦 上。現在每個人的電腦都有一個完整的版本庫,那多個人如何協作呢?比如說自己在電腦上改了檔案A,其他人也在電腦上改了檔案A,這時,你們兩之間只需把各自的修改推送給對方,就可以互相看到對方的修改了。
主要有如下特點:
1. 版本控制
2. 分散式
3. 工作過程是將伺服器上的程式碼下載到本地,本地開發完成後,在提交到伺服器端
git相比於svn功能更加的強大,命令也很多。本篇部落格將詳細介紹一些常用命令的使用操作。
Git幾個概念
一. 工作目錄
工作目錄是對專案的某個版本獨立提取出來的內容。這些從 Git 倉庫的壓縮資料庫中提取出來的檔案,放在磁碟上供你使用或修改。
二. 暫存區域
是一個檔案,儲存了下次將提交的檔案列表資訊,一般在 Git 倉庫目錄中。有時候也被稱作`‘索引’',不過一般說法還是叫暫存區域。
三. Git 倉庫目錄
是Git 用來儲存專案的元資料和物件資料庫的地方。這是 Git 中最重要的部分,從其它計算機克隆倉庫時,拷貝的就是這裡的資料。
Git工作流程
基本的 Git 工作流程如下:
在工作目錄中修改檔案 > 暫存檔案,將檔案的快照放入暫存區域 > 提交更新,找到暫存區域的檔案,將快照永久性儲存到 Git 倉庫目錄。
如果 Git 目錄中儲存著的特定版本檔案,就屬於已提交狀態。如果作了修改並已放入暫存區域,就屬於已暫存狀態。如果自上次取出後,作了修改但還沒有放到暫存區域,就是已修改狀態。
Git的安裝
第一種安裝方式:yum進行安裝
[[email protected] ~]# yum install git -y
第二種安裝方式:編譯安裝
第一步:上傳安裝包並解壓
[[email protected] ~]# rz [[email protected] ~]# ls | grep git git-v2.7.4.zip [[email protected] ~]# unzip git-v2.7.4.zip
第二步:安裝依賴
[[email protected] git-2.7.4]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y
第三步:進入解壓下目錄下並執行編譯
[[email protected] git-2.7.4]# make prefix=/usr/local/git all [[email protected] git-2.7.4]# make prefix=/usr/local/git install
第四步:匯入二進位制程式
[[email protected] git-2.7.4]# rm -rf /usr/bin/git [[email protected] git-2.7.4]# ln -s /usr/local/git/bin/git /usr/bin/git [[email protected] git-2.7.4]# git --version git version 2.7.4
演示1:簡單基礎演示
第一步:建立一個目錄,並進入
[[email protected] ~]# mkdir /kenken
[[email protected] ~]# cd /kenken
第二步:初始化目錄
[[email protected] kenken]# git init
第三步:建立一個測試檔案
[[email protected] kenken]# echo "this is ken">>a.tt
第四步:提交
[[email protected] kenken]# git add a.tt [[email protected] kenken]# git commit -m "v1"
第五步:檢視
[[email protected] kenken]# git log commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
第六步:往測試檔案裡面追加資料
[[email protected] kenken]# echo "new data" >>a.tt
第七步:提交
[[email protected] kenken]# git add a.tt [[email protected] kenken]# git commit -m "v2"
第八步:再次檢視
[[email protected] kenken]# git log commit d1f52146b390c95c614e8ae7acc922da0f7e2d0d Author: ken <[email protected]> Date: Tue Nov 20 10:34:11 2018 +0800 v2 commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
第九步:恢復到第v1版本
[[email protected] kenken]# git reset --hard HEAD~1 HEAD is now at 15370fe v1 [[email protected] kenken]# git log commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
第十步:恢復到v2版本
[[email protected] kenken]# git reflog #首先使用git reflog可以檢視commit值 15370fe [email protected]{0}: reset: moving to HEAD~1 d1f5214 [email protected]{1}: commit: v2 15370fe [email protected]{2}: reset: moving to HEAD~1 22d39cb [email protected]{3}: commit: v2 15370fe [email protected]{4}: commit (initial): v1 [[email protected] kenken]# git reset --hard d1f5214 #恢復的時候指定conmit值 HEAD is now at d1f5214 v2 [[email protected] kenken]# git log #檢視發現已經恢復到v2版本 commit d1f52146b390c95c614e8ae7acc922da0f7e2d0d Author: ken <[email protected]> Date: Tue Nov 20 10:34:11 2018 +0800 v2 commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
演示2:撤銷工作區的內容
第一步:建立一個檔案
[[email protected] kenken]# echo "test1">e.tt
[[email protected] kenken]# echo "test1">e.tt
第二步:把這個檔案新增到快取區
[[email protected] kenken]# git add e.tt
第三步:在檔案追加內容
[[email protected] kenken]# echo "test3">>e.tt
第四步:檢視工作區
[[email protected] kenken]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: e.tt # # 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: e.tt #
第五步:撤銷工作區的內容
使用命令git checkout -- filename即可進行回滾
[[email protected] kenken]# git checkout -- e.tt #資料回滾 [[email protected] kenken]# git status #檢視狀態 # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: e.tt # [[email protected] kenken]# cat e.tt #檢視檔案內容,發現test3內容已經沒有了 test1 test2
演示3:撤銷快取區的內容
第一步:建立檔案並進行提交
[[email protected] kenken]# echo "1111">k.tt [[email protected] kenken]# git add k.tt [[email protected] kenken]# git commit -m "k.tt" [[email protected] kenken]# git log commit c5f46907ce0dfd6722b091fa7a7053ff48507ace Author: ken <[email protected]> Date: Tue Nov 20 12:42:30 2018 +0800 k.tt
第二步:修改檔案並加入到快取區中
第一步一定要提交,才能看到效果
[[email protected] kenken]# echo "222">>k.tt [[email protected] kenken]# echo "333">>k.tt [[email protected] kenken]# git add k.tt [[email protected] kenken]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: k.tt #
第三步:快取區進行回退
[[email protected] kenken]# 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: k.tt # no changes added to commit (use "git add" and/or "git commit -a")
第四步:工作區進行回滾
其實現在就已經回到了演示2中了
回退完成之後可以發現之前寫的222和333 已經沒有了
[[email protected] kenken]# git checkout -- k.tt [[email protected] kenken]# git status # On branch master nothing to commit, working directory clean [[email protected] kenken]# cat k.tt 1111
演示4:撤銷倉庫中的內容
第一步:檢視歷史版本
我們現在處於k.tt中,現在希望回到v3中
[[email protected] kenken]# git log commit c5f46907ce0dfd6722b091fa7a7053ff48507ace Author: ken <[email protected]> Date: Tue Nov 20 12:42:30 2018 +0800 k.tt commit ec9c03a48c816aa08fdd2216effdd2f124f1b4e3 Author: ken <[email protected]> Date: Tue Nov 20 12:40:07 2018 +0800 v4 commit 1334466fd86ba0ba4ec95147809d71cb21345e95 Author: ken <[email protected]> Date: Tue Nov 20 12:28:11 2018 +0800 v3 commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
第二步:檢視所需要回滾的版本的commit值
通過上面我們可以看到v3的commit值為1334466fd86ba0ba4ec95147809d71cb21345e95
第三步:執行回滾操作
commit值沒有必要全部寫出來,只要寫一部分就可以了
[[email protected] kenken]# git reset --hard 1334466f
第四步:檢視版本狀態
現在我們處於v3版本之中
[[email protected] kenken]# git log commit 1334466fd86ba0ba4ec95147809d71cb21345e95 Author: ken <[email protected]> Date: Tue Nov 20 12:28:11 2018 +0800 v3 commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
第五步:回退到v4版本中
我們知道回退版本需要用到commit值
但是現在通過git log已經看不到v4的commit值了,現在怎麼辦吶?
可以通過git reflog檢視所有的歷史操作
找到v4前面的值即可
[[email protected] kenken]# git reflog 1334466 [email protected]{0}: reset: moving to 1334466f c5f4690 [email protected]{1}: commit: k.tt ec9c03a H[email protected]{2}: commit: v4 1334466 [email protected]{3}: commit: v3 15370fe [email protected]{4}: reset: moving to HEAD~1 d1f5214 [email protected]{5}: reset: moving to d1f5214 15370fe [email protected]{6}: reset: moving to HEAD~1 d1f5214 [email protected]{7}: commit: v2 15370fe [email protected]{8}: reset: moving to HEAD~1 22d39cb [email protected]{9}: commit: v2 15370fe [email protected]{10}: commit (initial): v1
第六步:回滾到v4版本
[[email protected] kenken]# git reset --hard ec9c03a HEAD is now at ec9c03a v4 [[email protected] kenken]# git log commit ec9c03a48c816aa08fdd2216effdd2f124f1b4e3 Author: ken <[email protected]> Date: Tue Nov 20 12:40:07 2018 +0800 v4 commit 1334466fd86ba0ba4ec95147809d71cb21345e95 Author: ken <[email protected]> Date: Tue Nov 20 12:28:11 2018 +0800 v3 commit 15370fed2791ba4c978018f840caed22fd38221f Author: ken <[email protected]> Date: Tue Nov 20 10:17:18 2018 +0800 v1
Git分支介紹
幾乎所有的版本控制系統都以某種形式支援分支。 使用分支意味著你可以把你的工作從開發主線上分離開來,以免影響開發主線。
Git 處理分支的方式可謂是難以置信的輕量,建立新分支這一操作幾乎能在瞬間完成,並且在不同分支之間的切換操作也是一樣便捷。 與許多其它版本控制系統不同,Git 鼓勵在工作流程中頻繁地使用分支與合併,哪怕一天之內進行許多次。
Git分支使用演示
第一步:檢視分支
*表示當前所處的分支位置
[[email protected] kenken]# git branch * master
第二步:建立分支
[[email protected] kenken]# git branch ken [[email protected] kenken]# git branch ken * master
第三步:切換分支
[[email protected] kenken]# git checkout ken Switched to branch 'ken' [[email protected] kenken]# git branch * ken master
第四步:在分支裡面操作e.tt
[[email protected] kenken]# ls a.tt c.tt d.tt e.tt [[email protected] kenken]# echo "888888">>e.tt [[email protected] kenken]# cat e.tt test1 test2 test3 test4 test5 888888
第五步:提交
git add .表示提交全部
[[email protected] kenken]# git add . [[email protected] kenken]# git commit -m "test for branch" [ken b621584] test for branch 1 file changed, 1 insertion(+)
第六步:切換回主分支並檢視檔案
現在檢視e.tt並沒有看到剛才子分支提交的內容
[[email protected] kenken]# git checkout master Switched to branch 'master' [[email protected] kenken]# git branch ken * master [[email protected] kenken]# cat e.tt test1 test2 test3 test4 test5
第七步:合併子分支
合併之後發現現在主分支也可以看到剛才子分支新增的內容了
[[email protected] kenken]# git merge ken Updating ec9c03a..b621584 Fast-forward e.tt | 1 + 1 file changed, 1 insertion(+) [[email protected] kenken]# cat e.tt test1 test2 test3 test4 test5 888888