git 忽略特定类型的所有文件,特定子文件夹中的文件除外

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

git ignore all files of a certain type, except those in a specific subfolder

gitgitignore

提问by Alex Wayne

I have a directory structure like this:

我有一个这样的目录结构:

  • root
    • folder1
      • abc.json
      • def.json
      • somedir
        • more.json
    • folder2
      • qwe.json
      • rty.json
    • spec
      • mock1.json
      • mock2.json
      • somedir
        • more_mocks.json
    • 文件夹 1
      • abc.json
      • 定义文件
      • 一些目录
        • 更多.json
    • 文件夹 2
      • qwe.json
      • rty.json
    • 规格
      • 模拟1.json
      • 模拟2.json
      • 一些目录
        • more_mocks.json

Now using a .gitignoreI want to ignore all *.jsonfiles except for the ones in the spec. I don't want to use a .gitignorein folder1and folder2because there are a ton of these and they will get added to a lot, and i'm sure I will forget to move the right .gitignorefile in place.

现在使用 a.gitignore我想忽略所有*.json文件,除了spec. 我不想使用.gitignorein folder1folder2因为有很多这样的文件,而且它们会被添加到很多中,我敢肯定我会忘记将正确的.gitignore文件移动到位。

In addition there may be more nested directories with json files, and this rule needs to apply to all subdirectories as well.

此外,可能会有更多带有 json 文件的嵌套目录,并且此规则也需要适用于所有子目录。

So how do I ignore all files of a type, except for a certain subdirectory?

那么如何忽略某个类型的所有文件,除了某个子目录?

回答by Amber

An optional prefix !which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.

!否定模式的可选前缀;任何被先前模式排除的匹配文件将再次包含在内。如果否定模式匹配,这将覆盖较低优先级的模式源。

http://schacon.github.com/git/gitignore.html

http://schacon.github.com/git/gitignore.html

*.json
!spec/*.json