.gitignore 并使用部分文件或文件夹匹配

.gitignore and using partial file or folder matching

我正在使用我的 .gitignore 和各种通配符来仅推送到存储库(与他人共享)特定文件夹或子文件夹。其中一些 .gitignore 语句涉及执行一般忽略和例外,忽略某些其他文件夹。例如。

Solved/ #this 防止任何名为 Solved 的文件夹被推送 Activities/ #这会阻止任何名为 Activities 的文件夹被推送 !01-Excel/Activities #this 允许此目录下的 Activities 文件夹被推送,尽管有上面的行。 !01-*/**/Activities #this 允许以“01-”开头的任何文件夹以及包含名为 Activities 的文件夹的子文件夹被推送,尽管有上面的行。

这是我的问题。

如何在 .git 中使用通配符以允许推送任何在其名称中有特定字符串的文件夹。

我有这样一个文件夹:

01-Excel/Activities/01-Ins_codeStart/Solved/
02-Excel/Activities/02-Stu_codeProgress/Solved/

我希望 git 忽略工作,以便推送任何目录下任何位置带有字符串“Ins”的已解决文件夹,而不会推送任何其他目录下的已解决文件夹。

这是我尝试过的:

!01-Excel/Activities/*-Ins*/Solved
!01-Excel/Activities//**-Ins*/Solved
!01-Excel/Activities//??-Ins*/Solved

None 的工作可以将 Solved 文件夹推送到名称中某处带有“Ins”的目录下。

我认识到这是一种杂草丛生的方式,但在我的情况下实现这一点的能力将节省大量时间。谢谢

Globbing 递归可以工作,尝试使用

01-Excel/Activities/**/*-Ins*/Solved

如果我理解你的问题是正确的,你想忽略文件夹 Solved 如果这个文件夹 Solved 不是包含字符串“Ins”的文件夹。

那么您必须在 .gitignore 文件中包含以下内容:

# ignore the folder Solved every where in this repository
**/Solved

# expect when the folder Solved is a subfolder of a folder where the string  "Ins" contains
!**/*-Ins*/Solved

None of those work to enable pushing of the Solved folder under directories with "Ins" somewhere in their name.

您可以使用 git check-ignore 检查原因:

git check-ignore -v -- /path/.../Ins/Solved/aFile

I would like the .gitignore to work such that the Solved folders under any directory with the string "Ins" anywhere in it are pushed, while the Solved folders under any other directory are not.

任何时候您需要将文件夹“列入白名单”时,都需要从 .gitignore 中排除所有父文件夹。

那是因为:

It is not possible to re-include a file if a parent directory of that file is excluded.

所以你不能忽略 Solved/ 文件夹 本身。
您可以忽略或不Solved/文件夹内容(意思是它的子文件和子文件夹)

# Exclude parent folders
!**/
# Ignore Solved folder content
Solved/**
# Exclude specific Solved folder content
!**/*-Ins*/Solved/**