我正在尝试git的一些示例指令,并遇到了这个特殊情况,当我们执行a时git rm *,它不会.*在第一次尝试时删除文件.为什么会这样?
mkdir -p test_repo1/folder && cd test_repo1
touch .testfile1 .testfile2 testfile3 folder/testfile4 folder/.testfile5
git init && git add . && git commit -m "test commit"
Run Code Online (Sandbox Code Playgroud)
如果现在我做了git rm如下,我必须再次删除所有文件
$ git rm -r *
rm 'folder/.testfile5'
rm 'folder/testfile4'
rm 'testfile3'
$ git rm -r *
rm '.testfile1'
rm '.testfile2'
Run Code Online (Sandbox Code Playgroud)
为什么git在第一次尝试中没有删除所有文件?另外,为什么仅对具有repo root的文件发生这种情况?
有趣的是,如果我只有那些.testfiles,那么git会在第一次尝试中删除它们.
我正在使用git版本 1.7.9.5
tri*_*eee 23
通配符由shell扩展,默认情况下,扩展在大多数shell中不包括点文件.
所以到时候git执行,第一个命令就变成了
git rm -r folder testfile3
Run Code Online (Sandbox Code Playgroud)
第二个可能是文字
git rm -r *
Run Code Online (Sandbox Code Playgroud)
它git然后通过自身扩张.
正如Keith所说,要一次性删除所有内容,请阻止shell扩展通配符,以便git第一次进行扩展.这可以使用双引号或单引号,或者在星号前使用反斜杠来完成.我倾向于选择单引号:
git rm -r '*'
Run Code Online (Sandbox Code Playgroud)
当你运行git rm *时,实际上是使用shell的globbing规则将参数传递给git-rm(1).默认情况下,许多shell不包含隐藏文件(例如带有前导点的文件).在Bash中,您可以使用dotglob和GLOBIGNORE更改globbing规则.例如:
使用内置的shopt:
# set dotglob
shopt -s dotglob
git rm *
# unset dotglob
shopt -u dotglob
Run Code Online (Sandbox Code Playgroud)使用GLOBIGNORE变量(也设置dotglob):
# Run git within a modified environment that includes GLOBIGNORE.
GLOBIGNORE='.git' git rm *
Run Code Online (Sandbox Code Playgroud)