最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - Can an asterisk * match nothing in .gitignore? - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Mar 13 at 7:47 Wiktor Stribiżew 628k41 gold badges498 silver badges615 bronze badges asked Mar 13 at 3:35 EasonEason 891 silver badge6 bronze badges 6
  • 4 foo/ matches the foo folder, so foo/* can only ever match anything inside that folder. foo is not inside itself. – Grismar Commented Mar 13 at 3:42
  • @Grismar Oh. I got it. I thought * could match nothing so foo/* can match foo/. 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:47
  • 3 What would it even mean to match "nothing"? If you want to match both foo itself and everything inside it, just add both foo/ and foo/* (although that's really not needed, just foo or foo/ would likely do). – Grismar Commented Mar 13 at 3:49
  • @Grismar I refer to "nothing" as no character. Yes, foo/ helps to ignore the directory foo and everything within. Just wanna clarify the pattern syntax. – Eason Commented Mar 13 at 6:50
  • Git doesn't track directories. What do you mean by "should have matched the foo directory in the root directory but it does not"? – choroba Commented Mar 13 at 7:39
 |  Show 1 more comment

1 Answer 1

Reset to default 3

As you point out, the .gitignore file uses glob expressions.

  • foo
    Matches anything named foo, so either a file or a folder named foo (and anything in it) would be ignored.

  • foo/
    Matches only a folder named foo, so it (and anything in it) would be ignored.

  • foo/*
    Matches anything inside the folder named foo, 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.

发布评论

评论列表(0)

  1. 暂无评论