版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pulong0748/article/details/82083872
首先理解下删除的几种情景:
- 删除本地工作区的文件 (某种意义来说是修改)
假如我本地有个hello.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: hello.txt
很明显此文件处于没有add的状态,还没有将删除操作保存到暂存区,也就意味着版本库还存在这个文件,假如你想撤回删除,可以使用chekout 命令:
$ git checkout hello.txt
发现状态又处于干净状态。
$ git status
On branch master
nothing to commit, working tree clean
- 删除git版本库中的文件
$ git rm hello.txt
$ git commit -m "删除文件"
查看一下状态:处于干净状态
$ git status
On branch master
nothing to commit, working tree clean
这时候你 checkout 已经回不去,可以通过回退版本的方式来找回删除的文件,所以还是要谨慎删除文件。