在Git中,刪除也是一個修改操做,咱們實戰一下,先添加一個新文件test.txt到Git而且提交:git
$ git add test.txt $ git commit -m "add test.txt"[master 94cdc44] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt
通常狀況下,你一般直接在文件管理器中把沒用的文件刪了,或者用rm
命令刪了:spa
$ rm test.txt
這個時候,Git知道你刪除了文件,所以,工做區和版本庫就不一致了,git status
命令會馬上告訴你哪些文件被刪除了:code
$ 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
:rem
$ 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
如今,文件就從版本庫中被刪除了。it
另外一種狀況是刪錯了,由於版本庫裏還有呢,因此能夠很輕鬆地把誤刪的文件恢復到最新版本:io
$ git checkout -- test.txt
git checkout
實際上是用版本庫裏的版本替換工做區的版本,不管工做區是修改仍是刪除,均可以「一鍵還原」。ast
命令git rm
用於刪除一個文件。若是一個文件已經被提交到版本庫,那麼你永遠不用擔憂誤刪,可是要當心,你只能恢復文件到最新版本,你會丟失最近一次提交後你修改的內容。test