Git教程-删除文件

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就是用版本库中的版本替换工作区中的版本,无论工作区是修改了还是被删除了,都可以一键还原。

但是!!!从来没有被添加到版本库就被删除的文件,是无法恢复的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章