GIT介紹
“Talk is cheap, Show me the code.” 說到git不得不提一下它的作者Linus-Linux神話的締造者,雖然才過不惑之年,但已經兩次改變了世界。咳咳...簡單膜拜之後,開始進入我們的正題吧 ~
git是分布式版本控制系統,這種優于集中式系統的架構是它流行原因之一。與SVN的集中式版本管理不同的是,git可以讓每個用戶端保留一個完整的版本,是以當開發者在進行自己的工作時,不必聯網,當自己的工作完成之後可以先送出到本地,等待有網絡的情況下再送出到伺服器。當然這隻是git的一個優點,在分支管理方面,git的表現也是其它系統無法望其項背的。
安裝部署GIT
GIT可以在我們常見的系統中安裝,但是作為一名忠實的Linux愛好者,我更傾向于直接在Linux上進行安裝。下面就簡單的了解一下Linux安裝方式.
這裡使用的是CentOS7的系統作為示例:
1
2
<code>[root@work ~]</code><code># uname -a</code>
<code>Linux work 3.10.0-327.28.2.el7.x86_64 </code><code>#1 SMP Wed Aug 3 11:11:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux</code>
檢視系統是否安裝git:
<code>[root@work ~]</code><code># git</code>
<code>-</code><code>bash</code><code>: git: </code><code>command</code> <code>not found</code>
沒有,那就裝上吧:
<code>[root@work ~]</code><code># yum install -y git</code>
由于是多人使用的版本控制系統,是以我們要标示上自己的個人賬戶:
<code>[root@work ~]</code><code># git config --global user.name "trying" # Your name</code>
<code>[root@work ~]</code><code># git config --global user.email "[email protected]" # Your Mail</code>
建立本地git 倉庫:
3
4
<code>[root@work ~]</code><code># mkdir gitrepo </code>
<code>[root@work ~]</code><code># cd gitrepo</code>
<code>[root@work gitrepo]</code><code># git init # 初始化git倉庫,會在此目錄生成一個.git的目錄</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/root/gitrepo/</code><code>.git/</code>
建立了本地的git 倉庫會生成一個.git的目錄,此目錄中記錄以後所有對代碼的改動資訊。值得注意的是,所有的版本控制系統隻能追蹤文本的改動資訊,如代碼,網頁,txt檔案等,對于視訊,圖檔這種二進制的檔案是無法追蹤其具體的變化的。
送出一個檔案到git 倉庫
在建立了一個git 倉庫之後,我們就可以嘗試添加一個檔案到我們的git倉庫中了。
建立一個檔案:
<code>[root@work gitrepo]</code><code># echo "Welcome to gitrepo! " > readme.txt</code>
添加一個檔案到倉庫:
<code>[root@work gitrepo]</code><code># git add readme.txt # 可以一次添加多個檔案,也分多次添加</code>
将檔案送出到倉庫:
<code>[root@work gitrepo]</code><code># git commit -m "my first git file"</code>
<code>[master (root-commit) 031cca7] my first git </code><code>file</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 1 insertion(+)</code>
<code> </code><code>create mode 100644 readme.txt</code>
執行了git commit之後,檔案就送出到倉庫了,-m 參數是對本此送出的一個說明,在實際的開發過程中,需要對每次送出的内容作說明,這樣更加友善識别和管理。
當對檔案再次改動:
<code>[root@work gitrepo]</code><code># echo "change it +1" >> readme.txt </code>
<code>[root@work gitrepo]</code><code># cat readme.txt </code>
<code>Welcome to gitrepo! </code>
<code>change it +1</code>
使用git status和git diff 指令可以時刻掌握檔案修改的狀态和内容:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<code>[root@work gitrepo]</code><code># git status</code>
<code># On branch master</code>
<code># Changes not staged for commit: # 顯示檔案已經被更改,但是還沒有送出。</code>
<code># (use "git add <file>..." to update what will be committed)</code>
<code># (use "git checkout -- <file>..." to discard changes in working directory)</code>
<code>#</code>
<code># modified: readme.txt</code>
<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>) </code><code># 提示需要add</code>
<code>[root@work gitrepo]</code><code># git diff # 檢視修改前後的内容,類似于linux的diff指令</code>
<code>diff</code> <code>--git a</code><code>/readme</code><code>.txt b</code><code>/readme</code><code>.txt</code>
<code>index 42c334d..238398e 100644</code>
<code>--- a</code><code>/readme</code><code>.txt</code>
<code>+++ b</code><code>/readme</code><code>.txt</code>
<code>@@ -1 +1,2 @@</code>
<code> </code><code>Welcome to gitrepo! </code>
<code>+change it +1 </code><code># 新添加的内容</code>
然後在對修改的檔案進行送出,同時檢視git status 在這個過程中的變化:
<code>[root@work gitrepo]</code><code># git add readme.txt </code>
<code>[root@work gitrepo]</code><code># git status # 執行add指令後,顯示需要commit這個檔案</code>
<code># Changes to be committed:</code>
<code># (use "git reset HEAD <file>..." to unstage)</code>
<code>[root@work gitrepo]</code><code># git commit -m "change +1"</code>
<code>[master 58a7726] change +1</code>
<code>[root@work gitrepo]</code><code># git status # 送出完成後,顯示工作目錄為空,沒有需要送出的内容。</code>
<code>nothing to commit, working directory clean</code>
版本控制與檔案修改
當我們使用git 送出了多個版本,需要檢視之前送出的版本資訊,可以使用git log指令檢視:
19
20
21
22
23
24
<code>[root@work gitrepo]</code><code># git log </code>
<code>commit c78d925cd2ce9836cd607089b958a48c8959b1d6</code>
<code>Author: trying <[email protected]></code>
<code>Date: Mon Dec 12 15:34:19 2016 +0800</code>
<code> </code><code>change +3 </code><code># 從近到遠,依次顯示送出的commit資訊</code>
<code>commit 8d255bedddd881e4fa70b2c2e6e9b6b14e54f41b</code>
<code>Date: Tue Aug 9 01:02:08 2016 +0800</code>
<code> </code><code>change +2</code>
<code>commit 58a7726b7e0f7a05b6b8077578e4085b423956d1</code>
<code>Date: Tue Aug 9 00:41:55 2016 +0800</code>
<code> </code><code>change +1</code>
<code>commit 031cca7e6021aee8c8a4fba49790c59c53990a4f</code>
<code>Date: Tue Aug 9 00:08:34 2016 +0800</code>
<code> </code><code>my first git </code><code>file</code>
git log指令顯示從最近到最遠的送出日志,如果想簡化輸出可以使用 --pretty=oneline參數:
<code>[root@work gitrepo]</code><code># git log --pretty=oneline</code>
<code>c78d925cd2ce9836cd607089b958a48c8959b1d6 change +3</code>
<code>8d255bedddd881e4fa70b2c2e6e9b6b14e54f41b change +2</code>
<code>58a7726b7e0f7a05b6b8077578e4085b423956d1 change +1</code>
<code>031cca7e6021aee8c8a4fba49790c59c53990a4f my first git </code><code>file</code>
檢視這些有什麼用呢,哈哈,相信聰明的你已經發行了,這裡輸出的日志中都包含了一個ID,通過這個ID 我們就可以回退到我們想要的版本,如我們要回退到上一個版本,git reset:
<code>[root@work gitrepo]</code><code># git reset --hard 8d255be</code>
<code>HEAD is now at 8d255be change +2</code>
<code>[root@work gitrepo]</code><code># cat readme.txt # 檢視檔案發現已經變回之前的内容</code>
<code>change +2</code>
這裡使用的是指定ID來回退到指點的版本,其實我們還有一個更加便捷的方法,可以使用HEAD^來回退:
<code>[root@work gitrepo]</code><code># git reset --hard HEAD^ # HEAD^前一個版本 HEAD^^前兩個版本</code>
<code>HEAD is now at 58a7726 change +1</code>
這裡用 “^” 來指定版本,幾個“^”就表示回退幾次,當然,如果回退的版本有點多可以使用HEAD~100,回退到100次版本前。
檢視日志:
<code>[root@work gitrepo]</code><code># git log --pretty=oneline</code>
通過檢視日志,我們發現我新編輯的更新沒有了,如果ID也沒記住,那豈不是回不到最新的内容了/(ㄒoㄒ)/~~ ,别擔心git都幫我們想到了,使用git reflog:
<code>[root@work gitrepo]</code><code># git reflog</code>
<code>58a7726 HEAD@{0}: reset: moving to HEAD^</code>
<code>8d255be HEAD@{1}: reset: moving to 8d255be</code>
<code>c78d925 HEAD@{2}: commit: change +3</code>
<code>8d255be HEAD@{3}: commit: change +2</code>
<code>58a7726 HEAD@{4}: commit: change +1</code>
<code>031cca7 HEAD@{5}: commit (initial): my first git </code><code>file</code>
發現了吧,我們所有的操作都被記錄了,這裡面就包含了每次操作的ID,這樣,我們又可以找到我們最新的版本,“快進” 到未來:
<code>[root@work gitrepo]</code><code># git reset --hard c78d925</code>
<code>HEAD is now at c78d925 change +3</code>
<code>change it 3</code>
這樣通過git reset --hard 和 git log,git reflog 指令我們可以讓檔案回到過去的任意狀态。
GIT緩存區和工作區
在使用git add 指令的時候,實際上是将我們的編輯的檔案送出到GIT的緩存區,然後再使用git commit 指令将檔案從緩存區送出到本地倉庫,如果不執行git add 添加檔案,那麼緩存區就不會記錄此檔案,後面再次更改檔案内容時就不會在執行git commit時送出。 如果對檔案進行了git add 之後,再次對檔案修改,需要重新執行git add 添加此檔案,最後執行git commit 的時候,會記錄最後一次git add 的檔案更改。
撤銷工作區修改内容:
有時會出現這樣一種情況,在我們的工作區(git倉庫目錄)修改了大量檔案,但是最後你發現檔案全部都改錯了! 是的,遇到這種尴尬的情況應該怎麼辦呢,回退到上一版本?顯然這樣不科學,尤其是本地沒有上一版本的時候,其實不必擔心,此時執行git status就能獲得提示:
<code># Changes not staged for commit:</code>
<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>
提示告訴我們要撤銷工作目錄改變的檔案可以使用git checkout -- <file>:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt #一條指令即可</code>
撤銷暫存區修改内容:
比上面更加尴尬的一種情況是,不僅修改了工作區的内容,而且還将錯誤的内容使用git add 送出到了暫存區,這時候怎麼辦呢,讓我們再看看git status 說什麼吧:
提示我們使用git reset HEAD <file>來從暫存區撤銷,試一下效果:
<code>[root@work gitrepo]</code><code># git reset HEAD readme.txt </code>
<code>Unstaged changes after reset:</code>
<code>M readme.txt</code>
<code>[root@work gitrepo]</code><code># git status # 檢視撤銷後的狀态,回到了git add之前的情況</code>
然後在再使用上面的指令,撤銷工作區的更改:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt </code>
這樣,所有的更改都已經撤銷了。
如果不幸執行了commit指令,可以通過版本復原版本來回到之前的狀态,但是如果已經推送到遠端倉庫,那就呵呵了。如果改動直接上線,可以通過持續內建伺服器,可以快速恢複,不過此時或多或少會影響到線上的業務。
删除檔案
如果我們想删除本地版本庫中的檔案,可以直接執行下面兩條指令:
<code>[root@work gitrepo]</code><code># rm -f aa.txt # 删除本地檔案</code>
<code>[root@work gitrepo]</code><code># git rm aa.txt # 删除版本庫檔案,并且commit送出</code>
<code>rm</code> <code>'aa.txt'</code>
<code>[root@work gitrepo]</code><code># git commit -m "del aa.txt"</code>
<code>[master 59f04b4] del aa.txt</code>
<code> </code><code>1 </code><code>file</code> <code>changed, 0 insertions(+), 0 deletions(-)</code>
<code> </code><code>delete mode 100644 aa.txt</code>
使用git rm <file> 和 git commit 來删除版本庫中的檔案。
但是如果本地檔案不小心删除錯了,隻要我們版本庫中送出過就可以快速恢複:
<code>[root@work gitrepo]</code><code># git checkout -- readme.txt</code>
<code>[root@work gitrepo]</code><code># rm -f readme.txt </code>
<code>[root@work gitrepo]</code><code># ll</code>
<code>total 0</code>
<code># (use "git add/rm <file>..." to update what will be committed)</code>
<code># deleted: readme.txt</code>
<code>total 4</code>
<code>-rw-r--r--. 1 root root 69 Dec 12 18:36 readme.txt </code><code># 檔案又回來了!</code>
本文轉自 酥心糖 51CTO部落格,原文連結:http://blog.51cto.com/tryingstuff/1882034