1 直接刪除倉庫裡面的檔案,檔案在staged裡面無變化。
git rm 11rm '11'
git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## deleted: 11#
接著提交,將變化更新到倉庫。git commit
[master 6eb8c6a] delete 11 v1.3 1 file changed, 1 deletion(-) delete mode 100644 11
2 刪除倉庫裡面的檔案11,但11已經被修改,還沒有被staged
$ git rm 11error: '11' has local modifications(use --cached to keep the file, or -f to force removal)
加上-f選項強制移除,--cahed的意思是會刪除staged裡面的檔案,但工作目錄下的對應的檔案會 保留。
$ git rm 11 -frm '11'$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## deleted: 11#
重新commit
[master 994b1ee] deleted 11 v1.3 0 files changed delete mode 100644 11
3 刪除staged裡面的檔案,但不刪除工作目錄下的對應檔案。
$ git status # On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## new file: 11#
$ git rm --cached 11rm '11'
$ git status # On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## 11nothing added to commit but untracked files present (use "git add" to track)
4 git mv 更該檔案名稱
$ git mv 11 12$ git status # On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## renamed: 11 -> 12#
相當於做了下面三個命令
$ mv 11 12$ git rm 11$ git add 12
用其他工具批處理改名的話,要記得在提交前刪除老的檔案名稱,再添加新的檔案名稱。
下面我接著手動更新12名至13名,再git status發現,Git 會意識到這是兩次連續的改名,11->12->13 ,很智能。
$ mv 12 13$ git rm 12rm '12'$ git add 13$ $ $ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## renamed: 11 -> 13#