Git教程-删除文件
删除文件
首先添加一个test.txt文件到Git中并提交:
$ git add test.txt
$ git commit -m "add test.txt"
[master 9bf969f] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
此时,在工作区中将该文件删除,或者用rm命令删除:$ rm test.txt
这时再次查看git status,Git会知道你删除了那些文件,工作区和版本库不一致了:
$ 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 "remove test.txt"
[master e2126a8] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
或者是误删了工作区中的文件,想要把版本库中的回复到工作区,用git checkout -- test.txt,git checkout就是用版本库中的版本替换工作区中的版本,无论工作区是修改了还是被删除了,都可以一键还原。
但是!!!从来没有被添加到版本库就被删除的文件,是无法恢复的!
博客介绍了在Git中文件的删除与恢复操作。先将test.txt文件添加到Git并提交,可在工作区手动或用命令删除文件,此时工作区与版本库不一致。若要在版本库删除文件,可使用相应命令;若误删工作区文件,可用版本库版本替换工作区版本实现还原。


被折叠的 条评论
为什么被折叠?



