Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I .gitignore files under a directory except ones with a pattern?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading