Gitignore排除所有子目录中的某些文件 [英] Gitignore exclude certain files in all subdirectories

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

问题描述

我有一个名为/ static的目录。其中有很多子目录。
除了.htaccess,null.jpg和index.php文件外,我需要忽略/ static /目录的所有子目录中的所有文件。
请告诉我这个操作的正确语法是什么?

  / static / ** 
! /static/**/.htaccess

  / static / * 
!/ static / * /。htaccess

不起作用。

解决方案

正如我在包括gitignore中的特定文件扩展名,要记住的主要规则是:

blockquote

如果排除该文件的父目录,则无法重新包含文件。 ( *

* 这是为什么任何忽略文件夹的规则(如 * * / )将会排除任何子文件不可能的情况。

这就是为什么正确的做法是排除除了以下内容之外的所有内容:


  • 文件夹,
  • 然后)要排除的文件。



如果您不首先排除文件夹,被忽略(因为我上面提到的规则)



因此,添加 .gitignore

  / static / ** / ** 
!/ static / ** /
!.gitignore
!.htaccess

使用Git 2.4.1进行测试,即使在Windows上也能正常运行。






请注意,使用git 2.9.x / 2.10(2016年中期?),如果父项可能重新包含文件该文件的目录排除在如果路径中没有通配符重新包含



NguyễnTháiNgọcDuy( pclouds 正试图添加此功能:



然而,其中一个条件是目录重新包含规则中的一部分必须是文字(即,没有通配符),你无法在这里使用该功能。


I have a directory called /static. There is a lot of subdirectories in it. I need to ignore all files in all subdirectories of the /static/ directory except of .htaccess, null.jpg and index.php files. Tell me please what is the right syntax for this operation?

/static/**
!/static/**/.htaccess

and

/static/*
!/static/*/.htaccess

don't work.

解决方案

As I mentioned in "Including specific file extension in gitignore", the main rule to remember is:

It is not possible to re-include a file if a parent directory of that file is excluded. (*)
(*: unless certain conditions are met in git 2.?+, see below)

That is why any rule which ignores folders (like * or */) would make excluding any sub-files impossible.

That is why the right approach is to exclude everything except:

  • folders,
  • (then) the files you want to exclude.

If you don't exclude folders first, your files would still be ignored (because of the rule I mention above)

So add in your .gitignore:

/static/**/**
!/static/**/
!.gitignore
!.htaccess

This is tested with Git 2.4.1 and works even on Windows.


Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

However, since one of the conditions was "The directory part in the re-include rules must be literal (i.e. no wildcards)", you cannot use that feature here anyway.

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

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