git ignore exception not working multiple subdirec

2019-06-05 23:20发布

问题:

I am having an issue where the exception syntax for gitignore (!fileName.txt) is not working when the file is multiple subdirectories below the ignored directory.

For instance:

web/[Ee]xample.[Ww]eb/[Ss]itecore/*
!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/[Cc]ustom/32x32*

does not include the files in the folder 32x32.
I have to manually add them via command line like so:

git add -f -r web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/[Cc]ustom/32x32

Is there a way to use the exception operator in a situation like this?

回答1:

Re-including a file that "is multiple subdirectories below the ignored directory" (as you try), does not work because "It is not possible to re-include a file if a parent directory of that file is excluded" [source].

As an ugly-but-working workaround, re-include all parent directories along the path before re-including your desired file, while keeping the files in these parent directories out by re-excluding them.

For your example it would like as follows:

web/[Ee]xample.[Ww]eb/[Ss]itecore/*

!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/
web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/**
!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/
web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/**
!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/
web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/**
!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/[Cc]ustom/
web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/[Cc]ustom/**
!web/[Ee]xample.[Ww]eb/[Ss]itecore/[Ss]hell/[Tt]hemes/[Ss]tandard/[Cc]ustom/32x32*

Source: my related answer here.

Alternatively, you could instead use .gitignore files that are placed in the directory of files to re-include [more here]. Depending on taste, this could be the prettier solution.



标签: git gitignore