Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

git rm * doesn't remove all files in one go

I was trying out some sample instructions of git and came across this peculiar case that when we do a git rm *, it doesn't delete the .* files in the first attempt. Why is it so?

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"

If now I do a git rm as follows, I have to do it a second time to remove all the files

$ git rm -r *
rm 'folder/.testfile5'
rm 'folder/testfile4'
rm 'testfile3'

$ git rm -r *
rm '.testfile1'
rm '.testfile2'

Why doesn't git remove all files in the first attempt itself? Also, why is this happening for files withing the repo root only?

Interestingly, if all I have are those .testfiles, then git removes them in the first attempt itself.

I am using git version 1.7.9.5

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The wildcard gets expanded by your shell, and the expansion does not include dot files, by default, in most shells.

So by the time git executes, the first command has become

git rm -r folder testfile3

and the second probably a literal

git rm -r *

which git then expands by itself.

As remarked by Keith, to remove everything in one go, prevent the shell from expanding the wildcard so that git does the expansion the first time already. This can be done with double or single quotes, or with a backslash before the asterisk. I tend to prefer single quotes:

git rm -r '*'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...