git 全局匹配中 ** 和 * 之间的区别(.gitignore)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8832729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:20:15  来源:igfitidea点击:

Difference between ** and * in glob matching (.gitignore)

gitshellglobgitignore

提问by Emil Ivanov

I have the following directory structure and files.

我有以下目录结构和文件。

pw-spec/
|-- event_spec.coffee
|-- event_spec.js
|-- integration
|   `-- service
|       |-- auth_spec.coffee
|       |-- auth_spec.js
|       |-- chat_spec.coffee
|       |-- chat_spec.js
|       |-- transport_spec.coffee
|       `-- transport_spec.js
|-- message_spec.coffee
|-- message_spec.js
|-- pw_spec.coffee
|-- pw_spec.js
|-- run.coffee
|-- run.html
|-- run.js
|-- service
|   |-- auth_spec.coffee
|   |-- auth_spec.js
|   |-- chat_spec.coffee
|   |-- chat_spec.js
|   |-- stream_spec.coffee
|   `-- stream_spec.js
|-- spec.coffee
|-- spec.js
`-- stub
    |-- stream.coffee
    |-- stream.js
    |-- transport.coffee
    `-- transport.js

4 directories, 27 files

I would like to ignore all *.jsfiles anywhere within pw-specdirectory.

我想忽略*.js目录中任何位置的所有文件pw-spec

However, adding the following patterns to .gitignoredoesn't cut it:

但是,添加以下模式.gitignore并不能削减它:

pw-spec/*.js
pw-spec/**/*.js

The problem is that the second one only matches js files that are exactly 1 level deep within the tree, while I want to match all js files under pw-spec.

问题是第二个只匹配树中深度为 1 级的 js 文件,而我想匹配pw-spec.

Doing

正在做

ls pw-spec/**/*.js

produces [1]:

产生 [1]:

pw-spec/service/auth_spec.js
pw-spec/service/chat_spec.js
pw-spec/service/stream_spec.js
pw-spec/stub/stream.js
pw-spec/stub/transport.js

As you can see

如你看到的

pw-spec/integration/service/auth_spec.js
pw-spec/integration/service/chat_spec.js
pw-spec/integration/service/transport_spec.js

are missing from [1].

[1] 中缺失。

采纳答案by Mischa Arefiev

The difference is that **doesn't work, at least not for everyone. See

不同之处在于**这行不通,至少对每个人都行不通。看

Why doesn't gitignore work in this case?

为什么 gitignore 在这种情况下不起作用?

You can have a separate .gitignorein pw-spec/

你可以有一个单独.gitignorepw-spec/

回答by Ben Martin

There are two approaches for this type of situation, depending on your needs.

对于这种情况,有两种方法,具体取决于您的需要。

One solution is to put

一种解决方案是把

# generated files
*.js

in pw-spec/.gitignore.

在 pw-spec/.gitignore 中。

The second solution is to put:

第二种解决方案是:

/pw-spec/*.js
/pw-spec/*/*.js
/pw-spec/*/*/*.js

and so forth in the main .gitignore file.This approach is brittle if more sub-directories are added.

在主 .gitignore 文件中等等。如果添加更多子目录,这种方法很脆弱。

I generally prefer to put the .gitignore file at the same level as the Makefile which generates the files that I am ignoring.

我通常更喜欢将 .gitignore 文件与生成我忽略的文件的 Makefile 放在同一级别。

回答by fge

Create a .gitignorein pw-specin which you insert these two lines:

创建一个.gitignorepw-spec其中插入这两行的文件:

*.js
*/*.js

Also note that if you already have files tracked in this subdirectory which you want "untracked", you have to make them unknown to the index as such:

另请注意,如果您已经在此子目录中跟踪了要“取消跟踪”的文件,则必须使它们不为索引所知:

git rm --cached path/to/file

For instance, if in directory pw-specyou can do:

例如,如果在目录中,pw-spec您可以执行以下操作:

find -type f -name "*.js" | xargs git rm --cached