忽略除某些特定文件,目录以外的所有文件 [英] Ignore all the files in the root except for some particular files, directories

查看:122
本文介绍了忽略除某些特定文件,目录以外的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这样做是必要的:

  * 
!/ dir1 /
!/ dir2 /
!/ file1
!/ file2

但令我惊讶的是,它没有。它似乎工作,如果我在星号前加上斜线。但我不明白为什么在这个变化之前它不起作用。任何人都可以指出吗?

解决方案

要使用否定模式 code> .gitignore ,只记得2条规则。

规则1.文件和目录在模式中分开处理。要包含一个目录,并不意味着它的子文件/目录也被包括在内。



I.E。目标是排除除 / dir1 之外的所有文件/目录。

 #无法工作
*
!/ dir1 /

为什么?原因只是第一条规则。虽然再次包含目录 dir1 ,但不是所有的子文件/目录。只有包含没有其子的目录意味着没有任何混帐,因为git不会跟踪空文件夹。



作为比较,以下工作。

 #works 
/ *
!/ dir1 /

为什么?由于 / * 仅直接匹配根目录下的文件/目录,但不会忽略子目录下的任何文件/目录,例如 dir1 这里。在!/ dir1 / 包含 dir1 后,它的所有子文件/目录都回来了。



有人会问,如果是这样,为什么以下不起作用,因为!/ dir1 / ** 会包含所有子文件/ $ dir1 回目录

 #无法工作
*
!/ dir1 / **

原因在于规则2。



规则2.如果它们的父目录仍然被忽略,它将不包含文件/目录。对于此规则,请阅读我首先回答一个SO问题



这就是为什么以前的模式不会工作。如果我们添加另一个否定模式来首先包含父目录 dir1 ,它就可以工作。

 #works 
*
!/ dir1 /
!/ dir1 / **






理解了这两条规则之后,OP案例的解决方案非常简单。

以下工作。

  / * 
!/ dir1 /
!/ dir2 /
!/ file1
!/ file2

以下内容也适用。

  * 
!/ dir1 /
!/ dir1 / **
!/ dir2 /
!/ dir2 / **
!/ file1
!/ file2

简化如下。

  * 
!* /
!/ dir1 / **
!/ dir2 / **
!/ file1
!/ file2


I supposed this to do the trick:

*
!/dir1/
!/dir2/
!/file1
!/file2

But to my surprise, it didn't. It seems to work if I prepend a slash before the asterisk. But I don't really understand why it doesn't work before this change. Could anybody point out?

解决方案

To use negating patterns ! in .gitignore, just remember 2 rules.

Rule 1. Files and directories are separatedly handled in the patterns. To include a directory back doesn't mean its child files/directories are also included back.

I.E. The goal is to exclude all files/directories except everything inside /dir1. The following does NOT work.

# CANNOT work
*
!/dir1/

Why? The reason is just the first rule. Though the directory dir1 is included again, but not all its child files/directories. And only including the directory without its children means nothing to git, since git won't track empty folders.

As a comparison, the following works.

# works
/*
!/dir1/

Why? Because /* only matches the files/directories under the root directory directly, but not ignores any files/directories under sub-directories such as dir1 here. After !/dir1/ includes dir1 back, all its child files/directories are back naturely.

Someone may ask, if so, why the following doesn't work, since !/dir1/** will includes all child files/directories of dir1 back?

# CANNOT work
*
!/dir1/**

The reason is in rule 2.

Rule 2. It won't include files/directories back if their parent directory is still ignored. For this rule, please read my answer to a SO question first.

That's why the previous patterns won't work. And if we add another negating pattern to include the parent directory dir1 back first, it works.

# works
*
!/dir1/
!/dir1/**


After understanding the 2 rules, the solutions for OP's case is quite straightforward.

The following works.

/*
!/dir1/
!/dir2/
!/file1
!/file2

And the following also works.

*
!/dir1/
!/dir1/**
!/dir2/
!/dir2/**
!/file1
!/file2

To make it simple as follows.

*
!*/
!/dir1/**
!/dir2/**
!/file1
!/file2

这篇关于忽略除某些特定文件,目录以外的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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