在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