在Git中,刪除也是一個修改操做。git
第一步,先添加一個新文件test.txt
到Git而且提交:spa
➜ testcase git:(master) touch test.txt ➜ testcase git:(master) ✗ git add test.txt ➜ testcase git:(master) ✗ git commit -m "add test.txt" [master a3ea391] add test.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt
通常狀況下,你一般直接在文件管理器中把沒用的文件刪了,或者用rm
命令刪了:code
➜ testcase git:(master) rm test.txt
這個時候,Git知道你刪除了文件,所以,工做區和版本庫就不一致了,git status
命令會馬上告訴你哪些文件被刪除了:blog
➜ testcase git:(master) ✗ 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
➜ testcase git:(master) ✗ git rm test.txt rm 'test.txt' ➜ testcase git:(master) ✗ git commit -m "remove test.txt" [master 359e5b0] remove test.txt 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test.txt
另外一種狀況是刪錯了,由於版本庫裏還有呢,因此能夠很輕鬆地把誤刪的文件恢復到最新版本:it
➜ testcase git:(master) ✗ git checkout -- test.txt ➜ testcase git:(master) ✗ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: test.txt
git checkout
實際上是用版本庫裏的版本替換工做區的版本,不管工做區是修改仍是刪除,均可以「一鍵還原」。io