gitignore除子文件夹中的文件外 [英] gitignore all except file in sub-sub folder

查看:97
本文介绍了gitignore除子文件夹中的文件外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过很多.gitignore的组合,但都没有为我需要的组合工作。

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

垃圾意味着任何其他文件。我只想提交config.xml文件,并忽略作业内的所有内容/除外。所以我试过:

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

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



我已经尝试了**,但我得到了同样的结果。



有什么想法?
Thanks!

解决方案

我在评论中提到的问题实际上回答了这种情况;关键部分如下:


如果一个目录被排除,Git将永远不会查看该目录的内容。


这只是对 gitignore 文档,重点是我的。


如果父母不能重新包含文件该文件的目录被排除。 由于性能原因,Git没有列出排除的目录,所以包含文件中的任何模式都不起作用,无论它们在何处定义。







您的模式 / jobs / * 会忽略作业。这意味着git甚至不会在这个被忽略的文件夹中查看你的!/ jobs / * / config.xml 模式是否与它们中的文件相匹配。



反过来,你必须明确 unignore 子文件夹,然后 reignore 内容;在此之后,您可以再次取消您的 config.xml 文件。这可能看起来很愚蠢,但这就是git句柄忽略的方式。

 #忽略/ jobs中的所有内容
/ jobs / *
#重新包含/ jobs中的所有文件夹
!/ jobs / * /
#忽略/ jobs子文件夹中的所有内容
/ jobs / * / *
#Reinclude config .xml文件位于/ jobs的第一级子文件夹中
!/ jobs / * / config.xml

这些模式将忽略作业中第一级子文件夹中的所有内容,但 config.xml / p>

I already tried many combinations of the .gitignore but none worked for what I need. I have this tree:

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

Garbage means any other file. I want to commit only the config.xml files, and ignore everything inside jobs/ except them. So I tried:

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

This way, everything inside jobs are ignored, including the config.xml file. With the inverse order, same happens. So, I can force add all config files and changes to them will be tracked, but if I add a new folder inside jobs, it's config.xml won't appear as a untracked file, so this way people can forgot to add them.

I already tried with **, but I got the same.

Any ideas? Thanks!

解决方案

The question I mentioned in the comments actually answers this scenario; the crucial part is the following:

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

Which is just a rephrasing of this snippet from the gitignore documentation, emphasis mine.

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.


Your pattern /jobs/* will ignore each file and folder in jobs. This means that git won't even look in this ignored folders to see if your !/jobs/*/config.xml pattern matches a file in them.

In turn you have to explicitly unignore the subfolders and then reignore the contents; after this you can again unignore your config.xml files. This might seem silly but that's how git handles ignores.

# 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

This patterns will ignore everything but config.xml files in the first-level subfolders of jobs.

这篇关于gitignore除子文件夹中的文件外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆