递归地忽略特定目录中的所有文件(.json文件除外) [英] Recursively ignore all files inside a specific directory except .json files
问题描述
我的文件结构类似于以下文件:
I have a file structure similar to the one below:
foo/bar.foo
node_modules/foo/bar.json
node_modules/foo/bar/foo.bar
我想做的是忽略node_modules
文件夹内除json
文件之外的所有文件,以便在存储库中得到以下文件结构:
What I want to do is ignore all the files inside the node_modules
folder except the json
files so that I end up with the following file structure in my repo:
foo/bar.foo
node_modules/foo/bar.json
我试图找到一种简单的方法来做到这一点,但我还没有到那儿.
I tried to find a simple way to do that but I'm not quite there yet.
这是我在.gitignore
中提出的内容:
Here's what I came up with in my .gitignore
:
# ignore everything inside node_modules
node_modules/*
# But descend into directories
!node_modules/**/*.json
达到期望结果的最优雅的方法是什么?
What's the most elegant way to achieve the desired result?
P.S.我不知道我在做什么.
P.S. I have no idea what I'm doing.
推荐答案
好,所以我终于找到了解决方案.这就是窍门:
Ok so I finally found a solution. Here's what did the trick:
# Ignore all the files inside the node_modules folder
node_modules/**/*.*
# Allow only json files inside the node_modules folder
!node_modules/**/*.json
问题在于通过执行node_modules/*
,它不仅会忽略文件,还会忽略文件夹.
The issue was that by doing node_modules/*
, it would not just ignore files but also folders.
正如git doc所说:
And as the git doc says:
如果文件的父目录不能重新包含该文件 文件被排除.
It is not possible to re-include a file if a parent directory of that file is excluded.
因此,我做了node_modules/**/*.*
,它仅排除文件,而不包括文件夹.
So instead I did node_modules/**/*.*
which only exclude files and not folders.
这样,!node_modules/**/*.json
实际上可以将json
文件列入白名单.
That way, !node_modules/**/*.json
is actually able to white list the json
files.
这篇关于递归地忽略特定目录中的所有文件(.json文件除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!