複習
在 git系列(一) 中知道了如何初始化版本倉庫,將檔案新增到倉庫以及將檔案提交到倉庫。對應的git命令分別為:
* git init
* git add -filename
* git commit -m「message」 :message是對當前提交做的解釋說明
以及用git status來檢視當前倉庫的狀態。
git status
過了兩天,我們繼續在原有的檔案上工作。我們用git status來檢視當前的倉庫狀態
changes not staged for commit:
(use
"git add ..."
to update what will be committed)
(use
"git checkout -- ..."
to discard changes in working directory)
modified: hello.txt
no changes added to commit (use
"git add"
and/or
"git commit -a")
它告訴我們hello.txt這個檔案被修改了。
git diff
但是由於週末兩天假期太浪,周一來的時候我忘記了在上乙個版本基礎之上做了哪些改動,這時候 git diff 命令起作用了。輸入git diff 命令
$ git diff
diff --git a/hello.txt b/hello.txt
index b56e903..a84e2d8 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1,3 @@
hello word
-hello git
\ no newline at end of file
+hello git
+git is a free software
\ no newline at end of file
在命令列中我們能清楚的看到,-hello git \ +hello git相當於這裡沒變,+git is a free software表示在原有的基礎之上我們又加上了這麼一行內容,知道了**做了改動後我們可以放心的繼續工作了。先將上禮拜的內容提交到倉庫
asus@asus-pc mingw64 /f/gitrepository/hellogit (master)
$ git add .
asus@asus-pc mingw64 /f/gitrepository/hellogit (master)
$ git commit -m"second commit"
[master 1229e00] second
commit
1 file changed, 2 insertions(+), 1 deletion(-)
上面沒用git add hello.txt
而是用git add .
來進行了代替。點.
代表了該資料夾下所有的改動檔案。
當然作為熱愛工作學習的你肯定每天都會提交新的東西到倉庫,到了星期三早上來開啟檔案內容已經是
hello git
git is a free software
today is monday . my plan is.....
today is tuesday .the weather is great
git log / git log –pretty=oneline
當然這裡的檔案內容只是作為演示功能使用,在實際生活中,不可能每次就一行內容改動,當改動內容很大,改動次數變多後你不可能記住每次的改動。這時候我們可以用git log
來檢視我們的操作記錄
asus@asus-pc mingw64 /f/gitrepository/hellogit (master)
$ git log
commit
3246df47611b37166c005794abad78c14242f02c (head -> master)
author: asus <[email protected]>
date: mon jul 9
23:18:41
2018 +0800
tuesday is great
commit
1a0e325ef3f6a4808836fe06707798561219c4c4
author: asus <[email protected]>
date: mon jul 9
23:16:34
2018 +0800
monday commit
commit
1229e00eaf7805de220ddfaa74116bb6e9b55719
author: asus <[email protected]>
date: mon jul 9
23:05:51
2018 +0800
second
commit
commit aec5e0cb32bde5f5335d441c34d3f7e1f431e756
author: asus <[email protected]>
date: wed jul 4
23:52:57
2018 +0800
first
commit
上面清楚的記錄著我們的提交記錄和提交注釋。commit 後面的那一大串數字記錄的是commit id,可以簡單理解為版本號。我們也可以用git log --pretty=oneline
命令將上述log精簡一下。
3246df47611b37166c005794abad78c14242f02c (head -> master) tuesday is great
1a0e325ef3f6a4808836fe06707798561219c4c4 monday commit
1229e00eaf7805de220ddfaa74116bb6e9b55719 second commit
aec5e0cb32bde5f5335d441c34d3f7e1f431e756 first commit
git reset
到了星期三早上,發現星期二的內容有點不對,想回退到星期一提交的那個版本。這時候常規操作就是直接上手刪掉星期二寫的內容,那麼這也太low了,而且這還得建立在你能記得那些內容是星期二加上去的前提下。
這時候只要用git reset --hard head^
命令就能回到上乙個版本啦。
$ git reset --hard head^
head is now at
1a0e325 monday commit
解釋下上面的命令,在git命令中head表示當前版本,head\^表示上乙個版本,head\^\^表示上上個版本。那麼同理前20個版本是不是要寫上20個\^呢?按道理來說可以是可以,但是一來這樣書寫不美觀二來容易出錯,這時候直接寫成 head~20,–hard代表什麼意思會在後期解釋。提交命令回退到星期一提交的那個版本後,檔案內容變為了
hello git
git is a free software
today is monday . my plan is.....
人總是善變的動物,剛回退到了星期一的版本不久後,又開始對星期二的內容念念不忘。好在git總能讓你有後悔藥吃,既能回到過去,又能去到將來。
如果這時候命令視窗還沒關,可以往上翻記錄找到星期二的那個commit id,我這上面的commit id是3246df47611…….,還是用回退命令git reset --hard 3246d
,commit id不用完全寫入,只需要輸入幾個字元就可以區分了。這時候你會發現檔案的內容又回到了星期二提交的那個版本。
如果命令視窗已經關了 ,還是可以用git reflog
命令檢視以往的git命令來找到commit id
asus@asus-pc mingw64 /f/gitrepository/hellogit (master)
$ git reflog
1a0e325 (head -> master) head@: reset: moving to head^
3246df4 head@: reset: moving to 3246d
1a0e325 (head -> master) head@: reset: moving to head^
3246df4 head@: commit: tuesday is great
1a0e325 (head -> master) head@: commit: monday commit
1229e00 head@: commit: second commit
aec5e0c head@: commit (initial): first commit
總結
git reflog :用於檢視git命令的操作歷史
Git(二) 版本回退與分割槽概念
git具有 快照 功能,每當你覺得檔案修改到一定程度的時候,就可以 儲存乙個快照 這個快照在git中被稱為commit。一旦你把檔案改亂了,或者誤刪了檔案,還可以從最近的乙個commit恢復,然後繼續工作,而不是把幾個月的工作成果全部丟失。在git中,我們用git log命令檢視歷史記錄。比如,我們...
Git版本回退(二)
前文的版本回退操作是在工作區進行的。有時候你已經把修改的 放到了暫存區,但是你想回退暫存區的 該怎麼做?我們這次增加乙個檔案,名字叫license。然後使用git status來檢視工作區的狀態。可以看到,git告訴我們license是未被跟蹤的檔案,也就是說它不在當前的git版本倉庫中。因為這是我...
Git 七 版本回退
現在,你已經學會了修改檔案,然後把修改提交到git版本庫,現在,再練習一次,修改readme.txt檔案如下 git is a distributed version control system.git is free software distributed under the gpl.然後嘗試...