git教程--git版本庫的使用

向版本控制器提交文件

咱們已經成功地添加並提交了一個readme.txt文件,如今,是時候繼續工做了,因而,咱們繼續修改readme.txt文件,改爲以下內容:git

Git is a distributed version control system.
Git is free software.

如今,運行git status命令看看結果:app

$ 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

git status命令可讓咱們時刻掌握倉庫當前的狀態,上面的命令告訴咱們,readme.txt被修改過了,但尚未準備提交的修改。分佈式


 

雖然Git告訴咱們readme.txt被修改了,但若是能看看具體修改了什麼內容,天然是很好的。好比你休假兩週從國外回來,第一天上班時,已經記不清上次怎麼修改的readme.txt,因此,須要用git diff這個命令看看:工具

$ git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

git diff顧名思義就是查看difference,顯示的格式正是Unix通用的diff格式,能夠從上面的命令輸出看到,咱們在第一行添加了一個「distributed」單詞。spa

知道了對readme.txt做了什麼修改後,再把它提交到倉庫就放心多了,提交修改和提交新文件是同樣的兩步,第一步是git add命令行

$ git add readme.txt

一樣沒有任何輸出。在執行第二步git commit以前,咱們再運行git status看看當前倉庫的狀態:3d

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   readme.txt
#

git status告訴咱們,將要被提交的修改包括readme.txt,下一步,就能夠放心地提交了:版本控制

$ git commit -m "add distributed"
[master ea34578] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)

提交後,咱們再用git status命令看看倉庫的當前狀態:指針

$ git status
# On branch master
nothing to commit (working directory clean)

Git告訴咱們當前沒有須要提交的修改,並且,工做目錄是乾淨(working directory clean)的。日誌

如今,再練習一次,修改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 <askxuefeng@gmail.com>
Date:   Tue Aug 20 15:11:49 2013 +0800

    append GPL

commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
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

看看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 <askxuefeng@gmail.com>
Date:   Tue Aug 20 14:53:12 2013 +0800

    add distributed

commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Mon Aug 19 17:51:55 2013 +0800

    wrote a readme file

最新的那個版本append GPL已經看不到了!比如你從21世紀坐時光穿梭機來到了19世紀,想再回去已經回不去了,腫麼辦?

辦法其實仍是有的,只要上面的命令行窗口尚未被關掉,你就能夠順着往上找啊找啊,找到那個append GPLcommit id3628164...,因而就能夠指定回到將來的某個版本:

$ git reset --hard 3628164
HEAD is now at 3628164 append GPL

再當心翼翼地看看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,如今,你又能夠乘坐時光機回到將來了。

相關文章
相關標籤/搜索