.gitignore白名单目录及其内容

Ivo*_*eek 32 git gitignore

我正在尝试将我的Zend Framework 2供应商目录中的目录(及其内容)供应商列入白名单.

/ vendor中的原始.gitignore文件如下所示:

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
Run Code Online (Sandbox Code Playgroud)

现在我想将目录SupplierName列入白名单,我认为这不应该太难.我已经阅读了gitignore上的文档并尝试了以下配置:

首先尝试,在评论之后添加!SupplierName,我必须在此处添加白名单路径.

# Add here the vendor path to be whitelisted
!SupplierName
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
Run Code Online (Sandbox Code Playgroud)

在那之后,我执行git status了没有显示vendor/SupplierName目录.git add vendor/SupplierName显示以下消息:

您的.gitignore文件之一忽略以下路径:vendor/SupplierName

第二次尝试

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!SupplierName
!.gitignore
*
Run Code Online (Sandbox Code Playgroud)

在那之后,我执行git status了没有显示vendor/SupplierName目录.git add vendor/SupplierName显示以下消息:

您的.gitignore文件之一忽略以下路径:vendor/SupplierName

第三次尝试

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
!SupplierName
Run Code Online (Sandbox Code Playgroud)

在那之后,我执行git status了没有显示vendor/SupplierName目录.git add vendor/SupplierName 似乎工作.但是现在,当我想添加Module.php文件(以及一些其他文件,子目录等)时,会发生以下情况.git add vendor/SupplierName/Module.php- >

您的.gitignore文件之一忽略以下路径:vendor/SupplierName/Module.php

# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
*
!.gitignore
!SupplierName
!SupplierName/
!SupplierName/*
Run Code Online (Sandbox Code Playgroud)

允许我直接在vendor/SupplierName中添加文件,但git add vendor/SupplierName/config/module.config.php仍然会导致

一个.gitignore文件忽略以下路径:vendor/SupplierName/config/module.config.php

我一直在寻找有关递归白名单的问题,因为这似乎是问题,但没有出现.

Tux*_*ude 37

您可以使用2个.gitignore文件来获得所需的结果:

# vendor/.gitignore
*
!.gitignore
!SupplierName/
!SupplierName/*

# vendor/SupplierName/.gitignore
!*
Run Code Online (Sandbox Code Playgroud)

我用测试回购测试了这个,似乎对我来说,在vendor/SupplierName目录下面的多个级别添加文件.

$ git add .

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   vendor/.gitignore
#   new file:   vendor/SupplierName/.gitignore
#   new file:   vendor/SupplierName/a
#   new file:   vendor/SupplierName/b
#   new file:   vendor/SupplierName/c
#   new file:   vendor/SupplierName/d
#   new file:   vendor/SupplierName/dir1/d
#   new file:   vendor/SupplierName/dir1/dir4/dir5/dir6/dir7/dir8/dir9/dir10/somefile
#   new file:   vendor/SupplierName/dir1/dir4/f1
#   new file:   vendor/SupplierName/dir1/dir4/f2
#   new file:   vendor/SupplierName/dir1/dir4/f3
#   new file:   vendor/SupplierName/dir1/dir4/f4
#   new file:   vendor/SupplierName/dir1/e
#   new file:   vendor/SupplierName/dir1/f
#   new file:   vendor/SupplierName/dir3/dir6/f5
#   new file:   vendor/SupplierName/dir3/dir6/f6
#   new file:   vendor/SupplierName/dir3/dir6/f7
#   new file:   vendor/SupplierName/dir3/dir7/f8
#   new file:   vendor/SupplierName/e
#
Run Code Online (Sandbox Code Playgroud)


Chr*_*ial 14

您也可以只使用一个.gitignore文件(在项目根目录中)实现此目的:

/*
!/.gitignore
!/vendor
/vendor/*
!/vendor/SupplierName
Run Code Online (Sandbox Code Playgroud)


Sim*_*ang 6

找到了有趣的文章:https : //jasonstitt.com/gitignore-whitelisting-patterns

所有学分归Jason Stitt所有。文本是从上面的网站复制的:

忽略所有内容,然后添加特定的子树

# Ignore everything
*
# But descend into directories
!*/
# Recursively allow files under subtree
!/subtree/**
# You can be specific with these rules
!/some/other/deep/path/**
!.gitignore 
Run Code Online (Sandbox Code Playgroud)

!*/规则将忽略所有目录。但是Git不跟踪目录,仅跟踪文件,因此!*/,它本身仅允许下降到完整的目录树中。它实际上不会允许任何东西进入仓库。有了该规则,您只需使用一个**递归通配符就可以包含一个子树。

如果不使用!*/,则需要其他规则来取消忽略/ subtree /及其子目录。

并非所有人都喜欢,!*/因为这意味着如果任何其他规则允许在您不想在存储库中找到的某个目录中找到文件名模式,则该目录本身将不会被阻止。您需要对文件使用特定的规则以使其包含在其中。

忽略根目录,然后添加整个子树

# Ignore everything in the root
/*
# Un-ignore all of subtree
!/subtree/
!.gitignore 
Run Code Online (Sandbox Code Playgroud)

这种模式比以前的模式要粗糙一些。该/*规则将仅忽略存储库目录结构根目录中的项目,因此,一旦将目录列入白名单,即使不使用***通配符,也将允许该目录的所有内容。

忽略目录中的所有内容,但保留空目录

*
!.gitignore
Run Code Online (Sandbox Code Playgroud)

Git不想在仓库中包含一个空目录,因为它会跟踪文件。将隐藏文件(例如.gitignore)放入目录中,它将被保存。但是要保持目录为空,即使您在其中具有用于测试/开发目的的文件,也最好忽略.gitignore文件本身以外的所有内容。