Git管理的是修改,而非文件本身。增加、刪除、甚至是創(chuàng)建新文件都是修改。
Git既可以管理修改,也可以撤銷修改。
下面我們分別看看這兩部分內(nèi)容。
管理修改
這個時候,可能有人不明白,到底什么是管理修改呢?我們來舉個小栗子看看哈。
對之前的love.txt
文件進行如下修改:
I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
Today is Tuesday.
之后添加文件到版本庫,并且查看此時的狀態(tài)。
git add love.txt
git status
命令行回復如下:
On branch master
Changes to be committed:
(use “git reset HEAD <file>…” to unstage)
modified: love.txt
再修改love.txt
如下:
I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
I know today is Tuesday.
現(xiàn)在,提交文件到版本庫:
git commit -m “Tuesday”
命令行回復如下:
[master 7238888] “Tuesday”
1 file changed, 2 insertions(+), 1 deletion(-)
現(xiàn)在,再次查看狀態(tài):
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: love.txt
no changes added to commit (use “git add” and/or “git commit -a”)
很明顯,第二次的修改沒有被提交。因為我們在第二次修改后沒有使用add
命令,修改并沒有進入緩存區(qū),所以提交的時候沒有第二次修改的內(nèi)容。
現(xiàn)在,我們用git diff HEAD — love.txt
命令查看一下版本庫中最新版本和工作區(qū)的區(qū)別:
git diff HEAD -- love.txt
命令行回復如下:
*diff —git a/love.txt b/love.txt*
*index e88da71..8347d74 100644*
*— a/love.txt*
*+++ b/love.txt*
@@ -2,4 +2,4 @@ I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
-Today is Tuesday.
\ No newline at end of file
+I know today is Tuesday.
\ No newline at end of file
版本庫中最新版本和工作區(qū)中存放的版本的區(qū)別一目了然,第二次修改沒有被提交。
撤銷修改
有修改,就有可能犯錯。這一輩子誰還沒有犯糊涂的時候呢?知錯能改善莫大焉。咱來看看如何改這錯誤。
假如,你在love.txt
中無意添加了一句不太好的言語:
I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
I know today is Tuesday.
I hate Lilei.
過了一會兒,你突然覺得這樣不妥。怎么辦?咱先git status
看看具體情況再說。
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: love.txt
no changes added to commit (use “git add” and/or “git commit -a”)
其實,答案已經(jīng)出現(xiàn)啦。Git告訴我們,使用git checkout - <file>
可以丟棄工作區(qū)的修改內(nèi)容。
我們來試試:
git checkout --love.txt
此時查看文件內(nèi)容:
cat love.txt
命令行輸出如下:
I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
Today is Tuesday.
我們又回到了最初遇見的地方~
這個時候有人可能有這樣的疑問:如果我不僅修改了內(nèi)容,還提交到了緩存區(qū),想撤銷,怎么破?
所謂車到山前必有路,咱們試試看唄~
現(xiàn)在,我們添加了一行不妥的話,并且git add
到了緩存區(qū)。此時,使用cat <file>
命令查看一下文本中的內(nèi)容:
cat love.txt
命令行顯示如下:
I love you so much as the mouse like rice.
Do you know?
Every day
Hey,Julie.
Today is Tuesday.
Lilei is a pig.
不幸中的萬幸是,在commit
之前發(fā)現(xiàn)了錯誤,我們再發(fā)git status
,問問Git有什么靈丹妙藥。
git status
Git告訴我們?nèi)缦拢?/p>
On branch master
Changes to be committed:
(use “git reset HEAD <file>…” to unstage)
modified: love.txt
Git很清楚地告訴我們,用git reset HEAD <file>
命令可以撤銷掉添加到緩存區(qū)的修改。
git reset HEAD love.txt
此時,命令行輸出:
Unstaged changes after reset:
M love.txt
此時,查看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: love.txt
no changes added to commit (use “git add” and/or “git commit -a”)
發(fā)現(xiàn)了嗎?如果你此時不知道需要什么指令的時候,不妨試一試git status
指令,Git會告訴你接下來有可能出現(xiàn)什么情況,并且告訴你應該怎么應對。
如果此時,你不僅添加到緩存區(qū),并且提交到版本庫,怎么破?請參照第四小節(jié)內(nèi)容(穿越歷史)。溫故而知新,可以為師矣。
本小節(jié)內(nèi)容總結(jié):
- 修改如果不適用
git add
命令進行添加到緩存區(qū)(stage),就不會被提交到版本庫中。git diff HEAD -- <fileName>
指令,可以查看當前版本庫中的版本和工作區(qū)中的版本有何不同?- 若修改了工作區(qū)內(nèi)容,還未添加到緩存區(qū),想撤銷,使用命令
git checkout --<file>
- 若修改了工作區(qū)內(nèi)容,并且已添加到緩存區(qū),但是未提交,想撤銷,可以使用命令
git reset HEAD <file>
。- 如果已經(jīng)將修改提交到了版本庫中,想撤銷,可以參照第四小節(jié)(穿越歷史)內(nèi)容。
- 友情提示:如果不知道該使用什么命令,可以使用
git status
,問問Git,它會告訴你接下來可能出現(xiàn)的情況以及對應的指令。
好啦。今天就到這里啦。下節(jié)再見哈~