Given that
- gitignore use Glob pattern instead of regular expression
- The official guide tells that an asterisk
*
matches anything except a slash/
I am wondering if an asterisk *
can match just nothing, which should be one situation of anything. If so, foo/*
should have matched the foo
directory in the root directory but it does not. What's the reason?
Given that
- gitignore use Glob pattern instead of regular expression
- The official guide tells that an asterisk
*
matches anything except a slash/
I am wondering if an asterisk *
can match just nothing, which should be one situation of anything. If so, foo/*
should have matched the foo
directory in the root directory but it does not. What's the reason?
1 Answer
Reset to default 3As you point out, the .gitignore
file uses glob expressions.
foo
Matches anything namedfoo
, so either a file or a folder namedfoo
(and anything in it) would be ignored.foo/
Matches only a folder namedfoo
, so it (and anything in it) would be ignored.foo/*
Matches anything inside the folder namedfoo
, including subfolders, but not the folder itself.
Having "anything inside" match 'nothing' makes no sense. Nothing isn't part of anything inside, and even if you somehow philosophically disagree, it's unclear what 'matching nothing' should mean technically, and how to ignore nothing.
The important difference is that with just foo
, you are also ignoring a file named foo
if it exists, while foo/
avoids that. And with foo/
you are ignoring the entire folder, while foo/*
only ignores its contents.
Consider this .gitignore
:
foo/
!foo/read.me
If you run git add .
from the project root, it will completely ignore foo/
and foo/read.me
won't be added, even though you said to exclude it from the ignored files. Git ignores it because it ignores the folder entirely.
Contrast with this .gitignore
:
foo/*
!foo/read.me
Now, if you run git add .
from the project root, it will ignore everything inside foo/
, except for foo/read.me
, which will be added to the repo.
Between foo
, foo/
, and foo/*
, you should be able to get whatever behaviour you need. If you were looking for some specific behaviour that none of the three gets you, please adjust your question to be specific about the different behaviour you need.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744721455a4589935.html
foo/
matches thefoo
folder, sofoo/*
can only ever match anything inside that folder.foo
is not inside itself. – Grismar Commented Mar 13 at 3:42*
could match nothing sofoo/*
can matchfoo/
. It sounds like the match process can be divided into two steps; enter the directory and match anything inside it. – Eason Commented Mar 13 at 3:47foo
itself and everything inside it, just add bothfoo/
andfoo/*
(although that's really not needed, justfoo
orfoo/
would likely do). – Grismar Commented Mar 13 at 3:49foo/
helps to ignore the directoryfoo
and everything within. Just wanna clarify the pattern syntax. – Eason Commented Mar 13 at 6:50foo
directory in the root directory but it does not"? – choroba Commented Mar 13 at 7:39