git gitignore 目录异常不起作用

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

gitignore directory exception not working

gitgitignore

提问by Marty Wallace

I have the following folder structure:

我有以下文件夹结构:

public
    media
        catalog
            product
            category
        private
        tmp
        var
        test

I want to gitignore everything in the mediadirectory except for catalog/categoryand private

我想 gitignoremedia目录中的所有内容,除了catalog/categoryprivate

My gitignore I am trying is:

我正在尝试的 gitignore 是:

public/media/*
!public/media/catalog/category/
!public/media/private

But it doesn't work. Any new files added to the categoryor privatedirectories are not available to add.

但它不起作用。添加到categoryprivate目录的任何新文件都无法添加。

I could just git add force but I would like this done through the gitignore if possible

我可以 git add force 但如果可能的话,我希望通过 gitignore 完成

回答by CB Bailey

It's usually simplest to put just a .gitignoreat the level where it starts to matter. (This also helps if you ever split a repository or move directories around.) In this case you need to ignore everything except catalogand privatein the public/mediafolder so in public/media/.gitignoreput:

通常最简单的方法是将 a.gitignore放在它开始重要的级别。(这也可以帮助,如果你曾经分裂围绕一个仓库或移动目录)。在这种情况下,你需要不顾一切,除了catalogprivatepublic/media这样的文件夹public/media/.gitignore放:

/*
!/catalog/
!/private/

and in public/media/catalog/.gitignoreput:

并输入public/media/catalog/.gitignore

/*
!/category/

It's important (and the reason that your rules are not working) not to ignore the public/media/catalogdirectory itself, as otherwise everything in it will be ignored, even if you didn't want to ignore a specific part of its contents.

重要的是(以及您的规则不起作用的原因)不要忽略public/media/catalog目录本身,否则其中的所有内容都将被忽略,即使您不想忽略其内容的特定部分。

Of course, you can combine this into a single ignore at the public/medialevel if you like:

当然,public/media如果您愿意,您可以在级别将其合并为单个忽略:

/*
!/catalog/
!/private/
/catalog/*
!/catalog/category/

回答by tanius

Solution with a single .gitignorefile in the repo root directory

使用.gitignorerepo根目录中的单个文件的解决方案

You tried to use one .gitignorefile at the repository's root level. Like you, I usually also centralize all ignores like this to keep things tidy. In this case it is difficult though, because "It is not possible to re-include a file if a parent directory of that file is excluded" [source].

您试图.gitignore在存储库的根级别使用一个文件。像你一样,我通常也会像这样集中所有忽略以保持整洁。在这种情况下,这很困难,因为“如果排除了该文件的父目录,则无法重新包含该文件”[来源]。

My ugly solution for this restriction, which I also explore in my related answers hereand here: re-include all parent directories along the path before re-including your desired file, while keeping other files in these parent directories out by re-excluding them.

我针对此限制的丑陋解决方案,我也在此处此处的相关答案中进行探讨:在重新包含所需文件之前,重新包含路径中的所有父目录,同时通过重新排除这些父目录中的其他文件.

With that, your .gitignorepatterns would be:

这样,您的.gitignore模式将是:

public/media/**
!public/media/private
!public/media/catalog/

public/media/catalog/**
!public/media/catalog/category/