gpt4 book ai didi

gitignore 除了子文件夹中的文件之外的所有文件

转载 作者:太空狗 更新时间:2023-10-29 14:00:04 27 4
gpt4 key购买 nike

我已经尝试了很多 .gitignore 的组合,但没有一个能满足我的需要。我有这棵树:

jobs/
jobs/projecta/config.xml
jobs/projecta/garbage
jobs/projecta/more/garbage
jobs/projectb/config.xml
jobs/projectb/garbage
jobs/projectb/more/garbage

垃圾是指任何其他文件。我只想提交 config.xml 文件,而忽略 jobs/除了它们之外的所有内容。所以我尝试了:

/jobs/*
!/jobs/*/config.xml

这样,作业中的所有内容都会被忽略,包括 config.xml 文件。使用相反的顺序,也会发生同样的情况。因此,我可以强制添加所有配置文件并跟踪对它们的更改,但如果我在作业中添加一个新文件夹,它的 config.xml 将不会显示为未跟踪文件,这样人们就可以忘记添加它们。

我已经用 ** 试过了,但还是一样。

有什么想法吗?谢谢!

最佳答案

question I mentioned in the comments实际上回答了这个场景;关键部分如下:

If a directory is excluded, Git will never look at the contents of that directory.

这只是对 gitignore 中的这个片段的重新措辞文档,强调我的。

It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.


您的模式 /jobs/* 将忽略 jobs 中的每个文件和文件夹。这意味着 git 甚至不会查看这个被忽略的文件夹以查看您的 !/jobs/*/config.xml 模式是否与其中的文件匹配。

反过来,您必须显式忽略子文件夹,然后重新忽略内容;之后您可以再次忽略您的config.xml 文件。这可能看起来很愚蠢,但这就是 git 处理忽略的方式。

# Ignore everything in /jobs
/jobs/*
# Reinclude all folders in /jobs
!/jobs/*/
# Ignore everything in the subfolders of /jobs
/jobs/*/*
# Reinclude config.xml files in the first-level subfolders of /jobs
!/jobs/*/config.xml

此模式将忽略除 jobs 的第一级子文件夹中的 config.xml 文件之外的所有内容。

关于gitignore 除了子文件夹中的文件之外的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29010443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com