在Git中取消忽略忽略目录的子目录 [英] Unignore subdirectories of ignored directories in Git

查看:596
本文介绍了在Git中取消忽略忽略目录的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我忽略了一个目录,但是我想在其中指定特定的子目录。所以我有设置:

  / uploads / 
/ uploads /垃圾/
/ uploads /垃圾/ stuff / KEEP_ME /
/ uploads / foo /
/ uploads / foo / bar / lose /

我想忽略所有内容,但 KEEP_ME 目录。我希望这个忽略看起来像这样:

  / uploads / * 
!/ uploads /垃圾/ stuff / KEEP_ME /

但这并不奏效,在同一个主题上也没有多少变化。



其中一项工作是

  / uploads / ** / ** / * * / 
!/ uploads /垃圾/ stuff / KEEP_ME /

限制性的和详细的?

解决方案

gitignore文档的模式格式部分


可选的前缀!否定了这种模式;任何匹配文件
将被重新包括在内。 如果该文件的父目录不包含
,那么重新包含文件不是
。 Git没有列出性能为
原因的排除目录,所以包含文件中的任何模式都不起作用,不管
在哪里定义。
将反斜杠(\)放入第一个
!的前面对于以文字!开头的模式,例如
!important!.txt。

之前排除的父目录 / uploads /垃圾/ stuff / keep / 模式必须在否定其内容之前完全否定:

  #ignore所有内容/ uploads / 
/ uploads / *

#include everything / within / uploads / garbage / stuff / keep
!/ uploads /垃圾/ stuff / keep /
!/ uploads /垃圾/ stuff / keep / *

要在 / uploads /垃圾/ stuff / keep / 中包含子目录,请添加第三行:

 !/ uploads /垃圾/ stuff / keep / ** / * 


Let's say I have ignored a directory, but I want to unignore specific subdirectories therein. So I have the setup:

/uploads/
/uploads/rubbish/
/uploads/rubbish/stuff/KEEP_ME/
/uploads/foo/
/uploads/foo/bar/lose/

And I want to ignore everything but the KEEP_ME directory. I would hope the ignore would look something like:

/uploads/*
!/uploads/rubbish/stuff/KEEP_ME/

But that's not working, and neither are several permutations on the same theme.

One that does work is

/uploads/**/**/**/
!/uploads/rubbish/stuff/KEEP_ME/

But that feels a little restrictive and verbose?

解决方案

According to pattern format section of the gitignore documentation:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. 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. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

Therefore the previously-excluded parent directory /uploads/rubbish/stuff/keep/ pattern must be exclusively negated before negating its content:

#ignore everything within /uploads/ 
/uploads/*

#include everything within /uploads/rubbish/stuff/keep
!/uploads/rubbish/stuff/keep/  
!/uploads/rubbish/stuff/keep/*

To include subdirectories inside /uploads/rubbish/stuff/keep/ add the third line:

!/uploads/rubbish/stuff/keep/**/*

这篇关于在Git中取消忽略忽略目录的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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