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就是用版本庫中的版本替換工作區中的版本,無論工作區是修改了還是被刪除了,都可以一鍵還原。

但是!!!從來沒有被添加到版本庫就被刪除的文件,是無法恢復的!

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