> For the complete documentation index, see [llms.txt](https://pshizhsysu.gitbook.io/git/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pshizhsysu.gitbook.io/git/ben-di-cang-ku/ban-ben-guan-li/shan-chu-wen-jian.md).

# 删除文件

在git中，删除也是一个修改操作， 我们来实战一下。首先，添加一个test.txt文件并向git提交

```
$ git add test.txt
$ git commit -m "initialize test.txt"
[master e175c41] initialize test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
```

然后，我们在shell下用rm直接删除该文件

```
$ rm -f 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 "delete test.txt"
[master b41a08d] delete test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt
```

现在，文件就从版本库中删除了

另一种情况是误删了文件，因为版本库中还有，所以我们能把它最新的版本从版本库中恢复出来

```
$ git checkout -- test.txt
```
