刪除文件

原文鏈接:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013758392816224cafd33c44b4451887cc941e6716805c000

在Git中,刪除也是一個修改操作,我們實戰一下,先添加一個新文件test.txt到Git並且提交:

$ vi test.txt
$ git add test.txt

$ git commit -m "add test.txt"
[master d831949] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

一般情況下,你通常直接在文件管理器中把沒用的文件刪了,或者用rm命令刪了:

$ rm test.txt

這個時候,Git知道你刪除了文件,因此,工作區和版本庫就不一致了,git status命令會立刻告訴你哪些文件被刪除了

$ 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 529de04] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

現在,文件就從版本庫中被刪除了。

 小提示:先手動刪除文件,然後使用git rm <file>和git add<file>效果是一樣的。

另一種情況是刪錯了,因爲版本庫裏還有呢,所以可以很輕鬆地把誤刪的文件恢復到最新版本:

$ git checkout -- test.txt

git checkout其實是用版本庫裏的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。

小結

命令git rm用於刪除一個文件。如果一個文件已經被提交到版本庫,那麼你永遠不用擔心誤刪,但是要小心,你只能恢復文件到最新版本,你會丟失最近一次提交後你修改的內容

 

 

 

 

 

 

 

 

 

 

 

 

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