Git rm命令几种使用方式

1 直接删除仓库里面的文件,文件在staged里面无变化。

git rm 11
rm '11'

git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." 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 11
error: '11' has local modifications
(use --cached to keep the file, or -f to force removal)
加上-f选项强制删除,--cahed的意思是会删除staged里面的文件,但工作目录下的对应的文件会 保留。
$ git rm 11 -f
rm '11'

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." 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 ..." to unstage)
#
#       new file:   11
#

$ git rm --cached 11
rm '11'

$ git status 
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       11
nothing 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 ..." 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 12
rm '12'
$ git add 13
$ 
$ 
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       renamed:    11 -> 13
#






你可能感兴趣的:(Git rm命令几种使用方式)