管理修改
現在,假定你已經完全掌握了暫存區的概念。下面,我們要討論的就是,為什么Git比其他版本控制系統設計得優秀,因為Git跟蹤並管理的是修改,而非文件。
你會問,什么是修改?比如你新增了一行,這就是一個修改,刪除了一行,也是一個修改,更改了某些字符,也是一個修改,刪了一些又加了一些,也是一個修改,甚至創建一個新文件,也算一個修改。
為什么說Git管理的是修改,而不是文件呢?我們還是做實驗。第一步,對readme.txt做一個修改,比如加一行內容:
test git modify second
study git
three add
four add
然后,添加:
$ git add readme.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
然后,再修改readme.txt:
test git modify second
study git
three add
four add modify
提交:
$ git commit -m "four modify"
[master de8fd65] four modify
1 file changed, 2 insertions(+), 1 deletion(-)
提交后,再看看狀態:
$ 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 add
-> 第二次修改 -> git commit
你看,我們前面講了,Git管理的是修改,當你用git add
命令后,在工作區的第一次修改被放入暫存區,准備提交,但是,在工作區的第二次修改並沒有放入暫存區,所以,git commit
只負責把暫存區的修改提交了,也就是第一次的修改被提交了,第二次的修改不會被提交。
提交后,用git diff HEAD -- readme.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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
可見,第二次修改確實沒有被提交。
注:
-- 使用其他參數(指定文件)可以提交未保存到暫存區(unstaged)
vi readme.txt
git commit readme.txt -m "commit with unstaged modify" vi readme.txt
git commit -a -m "commit all file will commit unstaged modify"
--不使用其他參數不會提交unstaged modify
git commit -m "commit without param will not commit unstaged modify"
git diff HEAD -- readme.txt 工作區文件和版本庫對比
git diff -- readme.txt 工作區和暫存區對比
那怎么提交第二次修改呢?你可以繼續git add
再git commit
,也可以別着急提交第一次修改,先git add
第二次修改,再git commit
,就相當於把兩次修改合並后一塊提交了:
第一次修改 -> git add
-> 第二次修改 -> git add
-> git commit
撤銷修改
自然,你是不會犯錯的。不過現在是凌晨兩點,你正在趕一份工作報告,你在readme.txt
中添加了一行:
test git modify second
study git
three add
four add modify
five add
現在不想要five add 了。你可以刪掉最后一行,手動把文件恢復到上一個版本的狀態。如果用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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
你可以發現,Git會告訴你,git checkout -- file
可以丟棄工作區的修改:
$ git checkout -- readme.txt
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作區的修改全部撤銷,這里有兩種情況:
- 一種是
readme.txt
自修改后還沒有被放到暫存區,現在,撤銷修改就回到和版本庫一模一樣的狀態; - 一種是
readme.txt
已經添加到暫存區后,又作了修改,現在,撤銷修改就回到添加到暫存區后的狀態。
總之,就是讓這個文件回到最近一次git commit
或git add
時的狀態。
現在,看看readme.txt
的文件內容:
$ cat readme.txt
test git modify second
study git
three add
four add modify
文件內容果然復原了。
git checkout -- file
命令中的--
很重要,沒有--
,就變成了“切換到另一個分支”的命令,我們在后面的分支管理中會再次遇到git checkout
命令。
現在假定是凌晨3點,你不但寫了一些胡話,還git add
到暫存區了:
test git modify second
study git
three add
four add modify
five add modify
six add modify
seven add modify
eight add modify ...
慶幸的是,在commit
之前,你發現了這個問題。用git status
查看一下,修改只是添加到了暫存區,還沒有提交:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
Administrator@PC-20160420ZEST MINGW32 /f/studygit (master)
Git同樣告訴我們,用命令git reset HEAD file
可以把暫存區的修改撤銷掉(unstage),重新放回工作區:
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
git reset
命令既可以回退版本,也可以把暫存區的修改回退到工作區。當我們用HEAD
時,表示最新的版本。
再用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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
還記得如何丟棄工作區的修改嗎?
$ git checkout -- readme.txt
$ git status
On branch master
nothing to commit, working directory clean
小結
場景1:當你改亂了工作區某個文件的內容,想直接丟棄工作區的修改時,用命令git checkout -- file
。
場景2:當你不但改亂了工作區某個文件的內容,還添加到了暫存區時,想丟棄修改,分兩步,第一步用命令git reset HEAD file
,就回到了場景1,第二步按場景1操作。
場景3:已經提交了不合適的修改到版本庫時,想要撤銷本次提交,參考版本回退一節,不過前提是沒有推送到遠程庫。
刪除文件
在Git中,刪除也是一個修改操作,我們實戰一下,先添加一個新文件test.txt(之前已經建立了)到Git並且提交。
一般情況下,你通常直接在文件管理器中把沒用的文件刪了,或者用rm
命令刪了:
$ rm test.txt
這個時候,Git知道你刪除了文件,因此,工作區和版本庫就不一致了,git status
命令會立刻告訴你哪些文件被刪除了:
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
現在你有兩個選擇,一是確實要從版本庫中刪除該文件,那就用命令git rm
刪掉,並且git commit
:
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
現在,文件就從版本庫中被刪除了。
另一種情況是刪錯了,只執行了git rm test.txt, 沒有執行git commit。因為版本庫里還有呢,所以可以很輕松地把誤刪的文件恢復到最新版本:
$ git checkout -- test.txt
如果只執行了git rm test.txt, 也執行git commit。可通過版本會退:
$ git reset --hard HEAD^
HEAD is now at dd34c9a no add but commit,because use other parameter
git checkout
其實是用版本庫里的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。
小結
命令git rm
用於刪除一個文件。如果一個文件已經被提交到版本庫,那么你永遠不用擔心誤刪,但是要小心,你只能恢復文件到最新版本,你會丟失最近一次提交后你修改的內容。