Including specific file extension in gitignore

2019-06-09 08:37发布

问题:

I want to include all the *.meta files in the Assets directory and all subdirectories while excluding everything else in Assets

I tried

/Assets/*
!/Assets/**/*.meta
!*.meta

but that only include *.meta within /Assets ????

Thanks for any help

回答1:

First, if an ignore rule does not work, you can check this with git check-ignore:

git check-ignore -v -- yourFile

It will display which ignore rule ignore a file that you thought should not be ignored.

Then the main rule to remember when it comes to ignore 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 your rules only include *.meta within /Assets (and not the subdirectories)

You would need to include all parent folders of a file (as in this recent example) in order to include a file

/Assets/**/**
! /Assets/**/
!/Assets/**/*.meta

I tested it with git 2.4.1 and it works (even on Windows).
It does include only the *.meta files in Assets and all subdirectories, but exclude everything else.


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:

  • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
  • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0-rc4.

However, since one of the condition to re-inclusion was:

The directory part in the re-include rules must be literal (i.e. no wildcards)

That would not have worked here.



标签: git gitignore