$ rm 只刪除工作區的檔案
$ git rm --cached 只刪除暫存區的檔案
$ git rm 刪除工作區和暫存區的檔案
為了方便理解,我將git倉庫repository定義為三個區域:
工作區:working diretory
暫存區:stage/index
提交區:master
下面這個圖展示了工作區、版本庫中的暫存區和提交區之間的關係。
首先建立乙個test資料夾作為倉庫,並建立readme.txt檔案,檔案內容:aaaaaaaaaaa;
$ mkdir test 建立test資料夾
$ cd test 進入test目錄
$ git init 建立倉庫,生成.git檔案
然後add並commit到版本庫中;
$ git add test.txt 向暫存區add檔案
$ git commit -m "aaaaaaaaaaaa" 向提交區commit檔案
[master (root-commit) f47ec85] aaaaaaaaaaaa
1 file changed, 1 insertion(+)
create mode 100644 test.txt
此時,執行$ rm test.txt
命令:
$ rm test.txt
執行該命令後,test檔案下的test.txt檔案消失,執行$ git status
命令:
$ git status
on branch master
changes not staged for commit:
(use
"git add/rm ..."
to update what will be committed)
(use
"git checkout -- ..."
to discard changes in working directory)
deleted: test.txt
no changes added to commit (use
"git add"
and/or
"git commit -a")
changes not staged for commit
表明刪除檔案和新增檔案對git來說都是一種改變,並沒有將此改變提交到暫存區。如果要在提交區也要刪除此檔案,那麼就要將此刪除改變先add到暫存區,然後再執行commit命令。
此時,執行$ git rm --cached test.txt
命令:
$ git rm --cached test.txt
rm 'test.txt'
則暫存區的test.txt檔案被刪除,執行$ git status
命令:
$ git status
on branch master
changes to be committed:
(use
"git reset head ..."
to unstage)
deleted: test.txt
untracked files:
(use
"git add ..."
to include in what will be committed)
test.txt
其中:untracked files
表明test.txt檔案已經沒有被暫存區跟蹤,即從暫存區刪除。
而changes to be committed
則表明此刪除改變已經提交到暫存區,因為直接在暫存區刪除了檔案,相當於將刪除操作直接add到了暫存區。
到這裡有兩種操作:
1. 不執行commit命令,執行git checkout head test.txt
,則可將提交區的test.txt檔案恢復到工作區和暫存區;
2. 執行commit命令,在提交區刪除該檔案,此時只有工作區還存在test.txt檔案。
此時,執行$ git rm test.txt
命令:
$ git rm test.txt
rm 'test.txt'
則工作區和暫存區的test.txt檔案均被刪除,執行$ git status
命令:
$ git status
on branch master
changes to be committed:
(use
"git reset head ..."
to unstage)
deleted: test.txt
據此可知,和$ git rm --cached test.txt
的第一種狀態一樣。
git rm 和 rm 的區別
用 git rm 來刪除檔案,同時還會將這個刪除操作記錄下來 用 rm 來刪除檔案,僅僅是刪除了物理檔案,沒有將其從 git 的記錄中剔除。直觀的來講,git rm 刪除過的檔案,執行 git commit m abc 提交時,會自動將刪除該檔案的操作提交上去。而對於用 rm 命令直接刪除的檔案,執...
git rm 和 rm 的區別
這是乙個比較膚淺的問題,但對於 git 初學者來說,還是有必要提一下的。用git rm來刪除檔案,同時還會將這個刪除操作記錄下來 用rm來刪除檔案,僅僅是刪除了物理檔案,沒有將其從 git 的記錄中剔除。直觀的來講,git rm刪除過的檔案,執行git commit m abc 提交時,會自動將刪除...
git rm 和rm 的區別
為了理解這兩個命令的區別,首先複習一下git的相關概念。git 倉庫目錄 是 git 用來儲存專案的元資料和物件資料庫的地方,可以理解為儲存著專案各個版本快照的大倉庫。工作目錄 是對專案的某個版本獨立提取出來的內容。暫存區域是乙個檔案,儲存了下次將提交的檔案列表資訊,假如在當前版本中有乙個檔案a,當...