Git學習總結四(刪除)
阿新 • • 發佈:2019-03-07
directory code clas 文件恢復 刪掉 master commit working cto
一般情況下,你通常直接在文件管理器中把沒用的文件刪了,或者用rm
命令刪了:
1 $ rm test.txt
這個時候,Git知道你刪除了文件,因此,工作區和版本庫就不一致了,git status
命令會立刻告訴你哪些文件被刪除了:
1 $ git status 2 On branch master 3 Changes not staged for commit: 4 (use "git add/rm <file>..." to update what will be committed) 5 (use "git checkout -- <file>..." to discard changes in working directory) 6 7 deleted: test.txt 8 9 no changes added to commit (use "git add" and/or "git commit -a")
現在你有兩個選擇,一是確實要從版本庫中刪除該文件,那就用命令git rm
刪掉,並且git commit
:
1 $ git rm test.txt 2 rm ‘test.txt‘ 3 4 $ git commit -m "remove test.txt" 5 [master d46f35e] remove test.txt6 1 file changed, 1 deletion(-) 7 delete mode 100644 test.txt
現在,文件就從版本庫中被刪除了。
另一種情況是刪錯了,因為版本庫裏還有呢,所以可以很輕松地把誤刪的文件恢復到最新版本:
1 $ git checkout -- test.txt
git checkout
其實是用版本庫裏的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。
Git學習總結四(刪除)