現在,你已經學會了修改文件,然後把修改提交到Git版本庫,現在,再練習一次,修改readme.txt文件如下:
Git is a distributed version control system.
Git is free software distributed under the GPL.
然後嘗試提交:
$ git add readme.txt
$ git commit -m "append GPL"
[master 3628164] append GPL
1 file changed, 1 insertion(+), 1 deletion(-)
像這樣,你不斷對文件進行修改,然後不斷提交修改到版本庫裏,就好比玩RPG遊戲時,每通過一關就會自動把遊戲狀態存盤,如果某一關沒過去,你還可以選擇讀取前一關的狀態。有些時候,在打Boss之前,你會手動存盤,以便萬一打Boss失敗了,可以從最近的地方重新開始。Git也是一樣,每當你覺得文件修改到一定程度的時候,就可以“保存一個快照”,這個快照在Git中被稱爲commit
。一旦你把文件改亂了,或者誤刪了文件,還可以從最近的一個commit
恢復,然後繼續工作,而不是把幾個月的工作成果全部丟失。
現在,我們回顧一下readme.txt文件一共有幾個版本被提交到Git倉庫裏了:
版本1:wrote a readme file
Git is a version control system.
Git is free software.
版本2:add distributed
Git is a distributed version control system.
Git is free software.
版本3:append GPL
Git is a distributed version control system.
Git is free software distributed under the GPL.
當然了,在實際工作中,我們腦子裏怎麼可能記得一個幾千行的文件每次都改了什麼內容,不然要版本控制系統幹什麼。版本控制系統肯定有某個命令可以告訴我們歷史記錄,在Git中,我們用git
log
命令查看:
$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <[email protected]>
Date: Tue Aug 20 15:11:49 2013 +0800
append GPL
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <[email protected]>
Date: Tue Aug 20 14:53:12 2013 +0800
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <[email protected]>
Date: Mon Aug 19 17:51:55 2013 +0800
wrote a readme file
git log
命令顯示從最近到最遠的提交日誌,我們可以看到3次提交,最近的一次是“append
GPL”,上一次是“add distributed”,最早的一次是“wrote a readme file”。 如果嫌輸出信息太多,看得眼花繚亂的,可以試試加上
--pretty=oneline
參數:
$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
需要友情提示的是,你看到的一大串類似3628164...882e1e0
的是commit
id
(版本號),和SVN不一樣,Git的commit id
不是1,2,3……遞增的數字,而是一個SHA1計算出來的一個非常大的數字,用十六進制表示,而且你看到的commit
id
和我的肯定不一樣,以你自己的爲準。爲什麼commit id
需要用這麼一大串數字表示呢?因爲Git是分佈式的版本控制系統,後面我們還要研究多人在同一個版本庫裏工作,如果大家都用1,2,3……作爲版本號,那肯定就衝突了。
每提交一個新版本,實際上Git就會把它們自動串成一條時間線。如果使用可視化工具查看Git歷史,就可以更清楚地看到提交歷史的時間線:
好了,現在我們啓動時光穿梭機,準備把readme.txt回退到上一個版本,也就是“add distributed”的那個版本,怎麼做呢?
首先,Git必須知道當前版本是哪個版本,在Git中,用HEAD
表示當前版本,也就是最新的提交3628164...882e1e0
(注意我的提交ID和你的肯定不一樣),上一個版本就是HEAD^
,上上一個版本就是HEAD^^
,當然往上100個版本寫100個^
比較容易數不過來,所以寫成HEAD~100
。
現在,我們要把當前版本“append GPL”回退到上一個版本“add distributed”,就可以使用git
reset
命令:
$ git reset --hard HEAD^
HEAD is now at ea34578 add distributed
--hard
參數有啥意義?這個後面再講,現在你先放心使用。
看看readme.txt的內容是不是版本“add distributed”:
$ cat readme.txt
Git is a distributed version control system.
Git is free software.
果然。
還可以繼續回退到上一個版本“wrote a readme file”,不過且慢,然我們用git
log
再看看現在版本庫的狀態:
$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <[email protected]>
Date: Tue Aug 20 14:53:12 2013 +0800
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <[email protected]>
Date: Mon Aug 19 17:51:55 2013 +0800
wrote a readme file
最新的那個版本“append GPL”已經看不到了!好比你從21世紀坐時光穿梭機來到了19世紀,想再回去已經回不去了,腫麼辦?
辦法其實還是有的,只要上面的命令行窗口還沒有被關掉,你就可以順着往上找啊找啊,找到那個“append GPL”的commit
id
是3628164...
,於是就可以指定回到未來的某個版本:
$ git reset --hard 3628164
HEAD is now at 3628164 append GPL
版本號沒必要寫全,前幾位就可以了,Git會自動去找。當然也不能只寫前一兩位,因爲Git可能會找到多個版本號,就無法確定是哪一個了。
再小心翼翼地看看readme.txt的內容:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
果然,我胡漢山又回來了。
Git的版本回退速度非常快,因爲Git在內部有個指向當前版本的HEAD
指針,當你回退版本的時候,Git僅僅是把HEAD從指向“append
GPL”:
改爲指向“add distributed”:
然後順便把工作區的文件更新了。所以你讓HEAD
指向哪個版本號,你就把當前版本定位在哪。
現在,你回退到了某個版本,關掉了電腦,第二天早上就後悔了,想恢復到新版本怎麼辦?找不到新版本的commit
id
怎麼辦?
在Git中,總是有後悔藥可以吃的。當你用$ git reset --hard HEAD^
回退到“add
distributed”版本時,再想恢復到“append GPL”,就必須找到“append GPL”的commit id。Git提供了一個命令git
reflog
用來記錄你的每一次命令:
$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file
終於舒了口氣,第二行顯示“append GPL”的commit id是3628164
,現在,你又可以乘坐時光機回到未來了。
小結
現在總結一下:
-
HEAD
指向的版本就是當前版本,因此,Git允許我們在版本的歷史之間穿梭,使用命令git reset --hard commit_id
。 -
穿梭前,用
git log
可以查看提交歷史,以便確定要回退到哪個版本。 -
要重返未來,用
git reflog
查看命令歷史,以便確定要回到未來的哪個版本。