Git-忽略子目录中的某些文件,但不是全部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6794717/
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-10 11:23:52  来源:igfitidea点击:

Git-ignore certain files in sub-directories, but not all

gitgitignore

提问by anonymous coward

I have a structure similar to the following:

我有一个类似于以下的结构:

/root/
/root/data/
/root/data/script.php
/root/data/some.json
/root/data/feature/one.json
/root/data/feature/two.json
/root/data/other-feature/one.json
/root/data/other-feature/important-script.php

I'd like git to ignore any.jsonfiles under the '/data/...' path, but '/data/' sometimes contains sub-directories.

我希望 git 忽略'/data/...' 路径下的任何.json文件,但 '/data/' 有时包含子目录。

My understanding is that a simple data/*.jsonin gitignore will only match one directory, as the *doesn't match /, as stated at http://git-scm.com/docs/gitignore, "Pattern Format", bullet #6:

我的理解是data/*.jsongitignore中的简单只会匹配一个目录,因为*不匹配/,如http://git-scm.com/docs/gitignore 中所述,“模式格式”,项目符号 #6:

Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

否则,git 将模式视为适合 fnmatch(3) 使用 FNM_PATHNAME 标志的 shell glob:模式中的通配符将不匹配路径名中的 /。例如,“Documentation/*.html”匹配“Documentation/git.html”但不匹配“Documentation/ppc/ppc.html”或“tools/perf/Documentation/perf.html”。

Is there a simple way to do this, or do I need to actively add gitignore files in each sub-directory, explicitly?

有没有一种简单的方法可以做到这一点,或者我是否需要明确地在每个子目录中主动添加 gitignore 文件?

回答by Ivan Danilov

I've written a post about such problem recently. See here.

我最近写了一篇关于这个问题的帖子。见这里

Basically what you need is to put one .gitignorewith *.jsonin the /data/directory.

基本上你需要的是把一个.gitignorewith*.json放在/data/目录中。

UPD: Since git 1.8.4(1.8.2if you're using msysgit) it is possible to use double-star patterns, like /data/**/*.json

UPD:由于 git 1.8.4(如果您使用的是 msysgit,则为1.8.2),因此可以使用双星模式,例如/data/**/*.json

回答by E.Monogarov

you can place data/**/*.jsonin your .gitignorein /rootdirectory to prevent multiple .gitignorefiles in different directories

您可以放置data/**/*.json在您的.gitignorein/root目录中以防止.gitignore不同目录中的多个文件

**/- matches any count of subdirectories (including current)

**/- 匹配任何数量的子目录(包括当前)

example: data/**/*.jsonrecord will ignore data/1.json, data/subfolder/2.json, data/../../3.json

例如:data/**/*.json记录将忽略data/1.json, data/subfolder/2.json,data/../../3.json

回答by Aqib Mumtaz

This pattern work for me for data subfolder ignoring only png and jpg files:

这种模式对我来说适用于仅忽略 png 和 jpg 文件的数据子文件夹:

**/data/**/*.png
**/data/**/*.jpg