我想白名单的目录(及其内容)SupplierName在我的Zend Framework 2供应商目录。
在/供应商原来的.gitignore文件看起来是这样的:
# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
现在,我想列入白名单这应该不是太难我以为目录SupplierName。 我已阅读gitignore的文档 ,并尝试了以下配置:
第一次尝试 ,加入!SupplierName它说,我要在这里添加白名单路径的评论之后。
# Add here the vendor path to be whitelisted
!SupplierName
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
之后当我执行git status
,其并未出现供应商/ SupplierName目录。 git add vendor/SupplierName
显示以下消息:
供应商/ SupplierName:下面的路径是通过你的.gitignore文件中的一个被忽略
第二次尝试
# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!SupplierName
!.gitignore
*
之后当我执行git status
,其并未出现供应商/ SupplierName目录。 git add vendor/SupplierName
显示以下消息:
供应商/ SupplierName:下面的路径是通过你的.gitignore文件中的一个被忽略
第三次尝试
# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
!.gitignore
*
!SupplierName
之后当我执行git status
,其并未出现供应商/ SupplierName目录。 git add vendor/SupplierName
似乎工作 。 但是现在,当我想要添加的Module.php文件(和其他一些文件,子目录等)发生以下情况。 git add vendor/SupplierName/Module.php
- >
供应商/ SupplierName / Module.php:下面的路径是通过你的.gitignore文件中的一个被忽略
# Add here the vendor path to be whitelisted
# Ex: for composer directory write here "!composer" (without quotes)
*
!.gitignore
!SupplierName
!SupplierName/
!SupplierName/*
让我可以直接在供应商/ SupplierName添加文件,但git add vendor/SupplierName/config/module.config.php
仍然导致
供应商/ SupplierName /配置/ module.config.php:下面的路径是通过你的.gitignore文件中的一个被忽略
我一直在寻找关于递归白名单的问题,因为这似乎是问题,但一无所获。
您可以使用2个.gitignore
文件,以达到预期的效果:
# vendor/.gitignore
*
!.gitignore
!SupplierName/
!SupplierName/*
# vendor/SupplierName/.gitignore
!*
我有一个测试回购测试此,似乎为我添加文件作为多层次深下的合作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
#
您也可以只用一个实现这一目标.gitignore
文件(在你的项目根):
/*
!/.gitignore
!/vendor
/vendor/*
!/vendor/SupplierName
发现一个有趣的文章: https://jasonstitt.com/gitignore-whitelisting-patterns
所有学分杰森·斯蒂特。 文章从网站上面复制:
不顾一切,然后添加特定的子树
# Ignore everything * # But descend into directories !*/ # Recursively allow files under subtree !/subtree/** # You can be specific with these rules !/some/other/deep/path/** !.gitignore
该!*/
治未忽略所有目录。 但Git不会追踪目录中,只有文件,所以!*/
本身将只允许陷入全面目录树; 它实际上不会让任何东西进入回购。 有了这条规则,你只能使用一个需要规则**
递归通配符,以包括子树。
如果您没有使用!*/
,你就需要更多的规则未忽略/树/及其子目录。
不是每个人都喜欢!*/
因为这意味着,如果有任何其他规则允许你不想在回购一些目录中发现了一个文件名模式,目录本身不会被阻塞。 您需要使用特定的规则文件与这一个包括。
忽略根目录,然后添加整个子树
# Ignore everything in the root /* # Un-ignore all of subtree !/subtree/ !.gitignore
这种模式是比前一个略显粗糙。 该/*
你白名单目录规则将只忽略回购的目录结构的根项目,使其尽快,所有的目录中的内容将被允许的,即使不使用*
或**
通配符。
忽略目录中的一切,但保留空目录
* !.gitignore
Git不会想在回购空目录,因为它跟踪文件。 将一个隐藏的文件(如的.gitignore)进入目录,它将被保存。 但是为了保持目录空的,即使你有文件在那里进行测试/开发目的,这是忽略除的.gitignore文件本身的一切是个好主意。
从CVS迁移到Git的时候我也有类似的问题。
不像CVS,Git不会看目录,它着重于文件。
例如,你不能没有,忽略目录“一”,但你不能,忽略目录中的所有文件“A”,例如:A / *
这同样适用于子目录如此。
如果目录“a”有一个子目录“b”和你忽略!“A / *”,那么你仍然会得到“A / B”的所有文件。
所以你就必须忽略太“!A / B / *”等的要白名单的所有子目录。
你只需要一个的.gitignore文件。
所以你最终的东西,如:
# ignore everything
*
# except for .gitignore files in the current directory
!.gitignore
# and all files in directory a
!a/*
#and all files in subdirectory b
!a/b/*
有了这个,你仍然会得到从A / C和A / B / C文件。 我不知道如果有一个递归下降子目录一种解决方法。
你应该包括在黑名单中的一切,然后再使在白名单中的每个目录和subdirctory。 例如,我只想把DIR /opt/source/
,DIR /opt/nginx/
和FILE /home/test/.opt/hello.txt
在白名单中,可以写.gitignore
类似这样的文件,以使其工作:
/*
!/.gitignore
!/opt
/opt/*
!/opt/source/
!/opt/nginx/
!/home
/home/*
!/home/test
/home/test/*
!/home/test/.opt
/home/test/.opt/*
!/home/test/.opt/hello.txt
我创建的能够在节点上运行产生一个白名单规则,因为我发现手工书写规则有点混乱,我希望能够修改后,如果我忘了怎么手工写它的规则简单的JS snipscript。
'use strict';
// Generating a "whitelist" wherein you only want a specific folder to be
// affected requires following .gitignore-style rules.
// https://github.com/prettier/prettier/issues/3328
//
// Handcrafting these rules is hard to reason about and error-prone, so I'm
// going to generate them.
// See: https://github.com/prettier/prettier/issues/3328
// And: https://git-scm.com/docs/gitignore
//
const path = require('path');
const whitelistDir = '/themes/simple/src/';
function generateIgnoreRule(dir) {
let output = '# Auto-generated by ' + path.basename(__filename) + '\n';
output += '# Exclude everything except `' + dir + '`\n';
// Add exclude everything rule
output += '/*' + '\n';
// Split by path
const parts = dir.split('/');
if (parts[0] === '') {
// Remove first item if its blank
parts.shift();
}
if (parts[parts.length - 1] === '') {
// Remove last item if its blank
parts.pop();
}
let totalPart = '';
for (let part of parts) {
totalPart += '/' + part;
output += '!' + totalPart + '\n';
if (part !== parts[parts.length - 1]) {
output += totalPart + '/*' + '\n';
}
}
return output;
}
console.log(generateIgnoreRule(whitelistDir));
console.log(
'\nCopy the above rules out of the console output and paste into your .gitignore / .prettierignore'
);