9.删除文件
[实践出真知]
创建test.txt 文件 并add 和commit到仓库
$ git status #新增加的文件test.txt On branch master Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track) $ git add test.txt #add 新的文件到暂存区(中转区) warning: LF will be replaced by CRLF in test.txt. The file will have its original line endings in your working directory.
$ git commit -m "add test.txt" #commit 从暂存区将新的文件提交到仓库.git [master 8c1d69f] add test.txt warning: LF will be replaced by CRLF in test.txt. The file will have its original line endings in your working directory. 1 file changed, 1 insertion(+) create mode 100644 test.txt
删除在工作区刚才新建的文件test.txt
$ rm test.txt #删除在工作区刚才新建的文件test.txt $ 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 #表示在工作区删除了一个文件:test.txt no changes added to commit (use "git add" and/or "git commit -a")
如下图
[看图说话]从上图中看出,当工作区的test.txt被删除的时候,可以使用git checkout -- <file> 来恢复test.txt这个文件
原理可以看成两种解释:
1、删除是一种修改,而git checkout -- <file> 就算恢复这种修改
2、删除后,git checkout -- <file>从.git仓库复制了一份test.txt到工作区
$ git checkout -- test.txt #恢复到修改(删除)前的状态
$ git status #查看状态
On branch master
nothing to commit, working directory clean
$ ls #查看test.txt文件是否恢复
LICENSE readme.txt test.txt
参考git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000