Imagine I have a config-files directory. I want all under this dir and subdirs except the ones with starting name of example. to be ignored.
I tried some approaches but none of them work and most of them result in all files under config-files directory to be untracked. Here are two examples of what I tried to do:
# Ignore everything in the config-files directory
/config-files/**
# Allow tracking of files starting with "example."
!/config-files/**/example.*
# Ignore everything in the config-files directory
/config-files/*
# Allow tracking of files starting with "example."
!/config-files/example.*
!/config-files/**/example.*
Note: the config-files itself is consisted of sub-directories like db, jwt and etc.
>Solution :
The key rule from the documentation is this:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
So if you have this tree:
.
└── config-files
└── a
├── example.txt
└── ignore-me.txt
Then the rule /config-files/* or /config-files/** ignores the path config-files/a, and git doesn’t even examine the files inside it.
To work around that, you need to specify rules that only match files, not whole directories. For instance:
# Ignore files inside the config-files sub-directories
/config-files/**/*.*
# Allow tracking of files starting with "example."
!/config-files/**/example.*
Now, the first rule will ignore the specific files config-files/a/example.txt and config-files/a/ignore-me.txt, and the second rule will re-include config-files/a/example.txt