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
就是用版本庫中的版本替換工作區中的版本,無論工作區是修改了還是被刪除了,都可以一鍵還原。
但是!!!從來沒有被添加到版本庫就被刪除的文件,是無法恢復的!