当.gitignore中的父文件夹被排除时,包含一个任意嵌套的子文件夹 [英] Git Including an arbitrarily nested subfolder when its parent has been excluded in .gitignore
问题描述
我要排除文件夹a及其子文件夹,但要包括一个特定的子文件夹b,其中b可以是a下方某个子文件夹,并且尚未指定到b的确切路径.
I'm excluding a folder a and its subfolders, but want to have a specific subfolder b included, where b can be a subfolder somewhere below a, and the exact path to b has not been specified.
我的.gitignore
包含以下行:
a/**
!a/**/b
但是这不起作用.尝试了上述方法的许多其他排列,包括来自堆栈溢出的其他文章,但似乎没有任何作用.
however this does not work. Have tried numerous other permutations of the above, including from other posts on stack overflow, but nothing appears to work.
关于stackoverflow的上一个问题涉及以下情况:可以显式声明子目录的路径.在此问题中,我要解决的情况是b
可以是a
下的任何子文件夹,而不是预定路径.
The previous question on stackoverflow deals with the situation where you can explicitly declare the path to the subdirectory. In this question, the situation I'm addressing is where b
can be any subfolder underneath a
, and not a predetermined path.
此外,较早的解决方案要求您取消忽略"每个父目录中您要取消忽略"的任何内容.但是,在这种情况下,我试图创建一个通用规则,该规则允许该子目录不影响其在目录树中的位置.因此,较早的解决方案在这种情况下不起作用/不适用.
Further the earlier solution requires you to "unignore" every parent directory of anything that you want to "unignore". However in this case I'm trying to create a generic rule that allows this subdirectory without bothering about where it is in the directory tree. So the earlier solution does not work/apply in this situation.
推荐答案
如果告诉git忽略目录,它将完全忽略该目录中的所有内容.这意味着git无法匹配您的排除对象,因为git根本不在关注它.
If you tell git to ignore a directory, it will completely ignore everything inside that directory. This means git cannot match your exclude because git is simply not looking at it.
以有意义的方式使用排除的唯一方法是针对单个目录,在该目录中,您将忽略除以下文件夹以外的所有内容:
The only way to use excludes in a meaningful way is for a single directory, where you ignore everything but some folder like this:
/some/path/*
!/some/path/foo
这将忽略除foo
直接位于/some/path
下的所有条目.
This will ignore all entries but foo
directly under /some/path
.
但是,在大多数情况下,只显式地忽略事物比使用排除要清楚得多.
But, most of the time it is much clearer to just explicitly ignore things than using excludes.
这篇关于当.gitignore中的父文件夹被排除时,包含一个任意嵌套的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!