作用:从git的索引库中删除对应文件的索引,如果目录树中存在索引记录,同时删除目录树中的记录,但不会删除本地文件。
常见的操作如下所示:
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--]
首先创建一个空的用于演示的文件,并执行git初始化工作
B000000079127B:StudioProjects test01$ mkdir gitTest
B000000079127B:StudioProjects test01$ cd gitTest
B000000079127B:gitTest test01$ git init
已初始化空的 Git 仓库于 /Users/test01/StudioProjects/gitTest/.git/
1. -f / --force
作用:从索引库中删除索引记录,并同时删除文件。执行完该命令后,需要通过commit进行提交或者通过git reset进行恢复。
实例
B000000079127B:gitTest test01$ echo "A">>A
B000000079127B:gitTest test01$ git add A
B000000079127B:gitTest test01$ git commit -m "create A"
[master(根提交) 8ce2b23] create A
1 file changed, 1 insertion(+)
create mode 100644 A
B000000079127B:gitTest test01$ git rm -f A
rm 'A'
B000000079127B:gitTest test01$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
删除: A
B000000079127B:gitTest test01$ git commit -m "保存删除A的记录"
B000000079127B:gitTest test01$ git log --oneline --abbrev-commit --graph
* 7b72c04 (HEAD -> master) 保存删除A的记录
* 8ce2b23 create A
B000000079127B:gitTest test01$ ls //因为A文件已经被删除,所以ls命令执行后不输出任何内容
B000000079127B:gitTest test01$ git ls-tree -l 8ce2b23
100644 blob f70f10e4db19068f79bc43844b49f3eece45c4e8 2 A
B000000079127B:gitTest test01$ git ls-tree -l 7b72c04 //因为A文件已经被删除,所以ls-tree命令执行后不输出任何内容
2. -n
作用:列出所有该命令行执行完以后涉及的文件,但是不会执行删除操作,不改变当前目录树的结构,无需commit或reset.
//创建B文件和C文件
B000000079127B:gitTest test01$ echo "B">>B
B000000079127B:gitTest test01$ echo "C">>C
B000000079127B:gitTest test01$ git add B
B000000079127B:gitTest test01$ git commit B -m "提交B文件"
[master effc90a] 提交B文件
1 file changed, 1 insertion(+)
create mode 100644 B
B000000079127B:gitTest test01$ git add C
B000000079127B:gitTest test01$ git commit C -m "提交C文件"
[master 2d19f60] 提交C文件
1 file changed, 1 insertion(+)
create mode 100644 C
//创建并提交完成
B000000079127B:gitTest test01$ git log --oneline --abbrev-commit --graph
* 2d19f60 (HEAD -> master) 提交C文件
* effc90a 提交B文件
* 7b72c04 保存删除A的记录
* 8ce2b23 create A
//执行-n操作 *表示通配符,下面的命令的作用范围是当前目录下的所有文件;
B000000079127B:gitTest test01$ git rm -n *
rm 'B'
rm 'C'
//查看执行-n操作后的状态
B000000079127B:gitTest test01$ git status
位于分支 master
无文件要提交,干净的工作区
//实际上并没有删除
B000000079127B:gitTest test01$ ls
B C
B000000079127B:gitTest test01$ git ls-tree 2d19f60
100644 blob 223b7836fb19fdf64ba2d3cd6173c6a283141f78 B
100644 blob 3cc58df83752123644fef39faab2393af643b1d2 C
3. -r
删除参数中文件夹下的对应的文件。
//创建一个名为subpath的文件夹,并在该文件夹中创建C和D两个文件
B000000079127B:gitTest test01$ mkdir subpath
B000000079127B:gitTest test01$ cd subpath
B000000079127B:subpath test01$ echo "C">>C
B000000079127B:subpath test01$ echo "D">>D
B000000079127B:subpath test01$ git add C
B000000079127B:subpath test01$ git commit -m "C"
[master c0cfe55] C
1 file changed, 1 insertion(+)
create mode 100644 subpath/C
B000000079127B:subpath test01$ git add D
B000000079127B:subpath test01$ git commit -m "D"
[master 992494e] D
1 file changed, 1 insertion(+)
create mode 100644 subpath/D
B000000079127B:subpath test01$ cd ..
B000000079127B:gitTest test01$ git status
位于分支 master
无文件要提交,干净的工作区
B000000079127B:gitTest test01$ git log --oneline --abbrev-commit --graph
* 992494e (HEAD -> master) D
* c0cfe55 C
* 2d19f60 提交C文件
* effc90a 提交B文件
* 7b72c04 保存删除A的记录
* 8ce2b23 create A
//不加-r时删除所有文件
B000000079127B:gitTest test01$ git rm .
fatal: 未提供 -r 选项不会递归删除 '.'
//加上-r
B000000079127B:gitTest test01$ git rm -r .
rm 'B'
rm 'C'
rm 'subpath/C'
rm 'subpath/D'
B000000079127B:gitTest test01$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
删除: B
删除: C
删除: subpath/C
删除: subpath/D
B000000079127B:gitTest test01$ git commit -m "循环删除-r"
[master d11c72b] 循环删除-r
4 files changed, 4 deletions(-)
delete mode 100644 B
delete mode 100644 C
delete mode 100644 subpath/C
delete mode 100644 subpath/D
B000000079127B:gitTest test01$ git log --oneline --abbrev-commit --graph
* d11c72b (HEAD -> master) 循环删除-r
* 992494e D
* c0cfe55 C
* 2d19f60 提交C文件
* effc90a 提交B文件
* 7b72c04 保存删除A的记录
* 8ce2b23 create A
B000000079127B:gitTest test01$ git ls-tree -l d11c72b
B000000079127B:gitTest test01$ ls
B000000079127B:gitTest test01$ ls
4. --cached
从索引中删除文件索引,文件不会被删除,git会提示该文件未被追踪。
//创建文件E
B000000079127B:gitTest test01$ echo "E">>E
B000000079127B:gitTest test01$ git rm --cached E
fatal: 路径规格 'E' 未匹配任何文件
//给文件E创建索引
B000000079127B:gitTest test01$ git add E
B000000079127B:gitTest test01$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
新文件: E
//删除E的索引
B000000079127B:gitTest test01$ git rm --cached E
rm 'E'
B000000079127B:gitTest test01$ git status
位于分支 master
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
E
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
//E文件并没有被删除
B000000079127B:gitTest test01$ ls
E
B000000079127B:gitTest test01$ cat E
E
//验证文件被提交以后也能被git rm删除索引
B000000079127B:gitTest test01$ git add E
B000000079127B:gitTest test01$ git commit -m "E"
[master 86e2a07] E
1 file changed, 1 insertion(+)
create mode 100644 E
B000000079127B:gitTest test01$ git rm --cached E
rm 'E'
B000000079127B:gitTest test01$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
删除: E
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
E
B000000079127B:gitTest test01$ git commit -m "删除E文件"
[master 979c014] 删除E文件
1 file changed, 1 deletion(-)
delete mode 100644 E
B000000079127B:gitTest test01$ git log --oneline --abbrev-commit --graph
* 979c014 (HEAD -> master) 删除E文件
* 86e2a07 E
* d11c72b 循环删除-r
* 992494e D
* c0cfe55 C
* 2d19f60 提交C文件
* effc90a 提交B文件
* 7b72c04 保存删除A的记录
* 8ce2b23 create A
B000000079127B:gitTest test01$ git ls-tree 979c014
B000000079127B:gitTest test01$ ls
E
B000000079127B:gitTest test01$ cat E
E
5.--ignorc-unmatch
忽略未匹配的文件。
B000000079127B:gitTest wuzepeng01$ ls
E
B000000079127B:gitTest wuzepeng01$ cat E
E
//当没有加--ignore-unmatch时,若删除没有追踪的文件,会报提示
B000000079127B:gitTest wuzepeng01$ git rm E
fatal: 路径规格 'E' 未匹配任何文件
//当加--ignore-unmatch时,若删除没有追踪的文件,不会报提示
B000000079127B:gitTest wuzepeng01$ git rm --ignore-unmatch E
B000000079127B:gitTest wuzepeng01$ ls
E
6. -q/ -quiet
执行删除索引成功后不输出提示。删除的是不存在的文件索引时仍然会提示。
B000000079127B:gitTest test01$ git add E
B000000079127B:gitTest test01$ git rm --cached -q E
B000000079127B:gitTest test01$ git status
位于分支 master
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
E
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
B000000079127B:gitTest test01$ ls
E
B000000079127B:gitTest test01$ cat E
E
//删除一个未被追踪的F(该文件目前不存在)还是会输出提示的
B000000079127B:gitTest wuzepeng01$ git rm --cache -q F
fatal: 路径规格 'F' 未匹配任何文件
7 --
用于隔离操作参数和文件参数