(Git學習)六、版本回退

現在,你已經學會了修改文件,然後把修改提交到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 1094adb] 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 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:06:15 2018 +0800

    append GPL

commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <[email protected]>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

git log命令顯示從最近到最遠的提交日誌,我們可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file

如果嫌輸出信息太多,看得眼花繚亂的,可以試試加上--pretty=oneline參數:

$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file

需要友情提示的是,你看到的一大串類似1094adb...的是commit id(版本號),和SVN不一樣,Git的commit id不是1,2,3……遞增的數字,而是一個SHA1計算出來的一個非常大的數字,用十六進制表示,而且你看到的commit id和我的肯定不一樣,以你自己的爲準。爲什麼commit id需要用這麼一大串數字表示呢?因爲Git是分佈式的版本控制系統,後面我們還要研究多人在同一個版本庫裏工作,如果大家都用1,2,3……作爲版本號,那肯定就衝突了。

每提交一個新版本,實際上Git就會把它們自動串成一條時間線。如果使用可視化工具查看Git歷史,就可以更清楚地看到提交歷史的時間線:

git-log-timeline

好了,現在我們啓動時光穿梭機,準備把readme.txt回退到上一個版本,也就是add distributed的那個版本,怎麼做呢?

首先,Git必須知道當前版本是哪個版本,在Git中,用HEAD表示當前版本,也就是最新的提交1094adb...(注意我的提交ID和你的肯定不一樣),上一個版本就是HEAD^,上上一個版本就是HEAD^^,當然往上100個版本寫100個^比較容易數不過來,所以寫成HEAD~100

現在,我們要把當前版本append GPL回退到上一個版本add distributed,就可以使用git reset命令:

$ git reset --hard HEAD^
HEAD is now at e475afc 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 e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <[email protected]>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

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

辦法其實還是有的,只要上面的命令行窗口還沒有被關掉,你就可以順着往上找啊找啊,找到那個append GPLcommit id1094adb...,於是就可以指定回到未來的某個版本:

$ git reset --hard 1094a
HEAD is now at 83b0afe 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

git-head

改爲指向add distributed

git-head-move

然後順便把工作區的文件更新了。所以你讓HEAD指向哪個版本號,你就把當前版本定位在哪。

現在,你回退到了某個版本,關掉了電腦,第二天早上就後悔了,想恢復到新版本怎麼辦?找不到新版本的commit id怎麼辦?

在Git中,總是有後悔藥可以吃的。當你用$ git reset --hard HEAD^回退到add distributed版本時,再想恢復到append GPL,就必須找到append GPL的commit id。Git提供了一個命令git reflog用來記錄你的每一次命令:

$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file

終於舒了口氣,從輸出可知,append GPL的commit id是1094adb,現在,你又可以乘坐時光機回到未來了。

小結

現在總結一下:

  • HEAD指向的版本就是當前版本,因此,Git允許我們在版本的歷史之間穿梭,使用命令git reset --hard commit_id

  • 穿梭前,用git log可以查看提交歷史,以便確定要回退到哪個版本。

  • 要重返未來,用git reflog查看命令歷史,以便確定要回到未來的哪個版本。

文章來源:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013744142037508cf42e51debf49668810645e02887691000

發佈了12 篇原創文章 · 獲贊 2 · 訪問量 6304
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章