1

I have the following project structure:

A
├── a.py
├── B
│   └── b.py
└── C

where I would like to ignore all contents of A, but include all *.py files recursively. My .gitignore looks like this:

A/*
!A/**/*.py

which does not seem to give the desired behavior - only a.py is tracked in this case. What am I missing? I would like to avoid defining subdirectory-specific rules like !A/B/*.py.

John Titor
  • 461
  • 3
  • 13
  • 2
    There is a nicer way to not ignore directories: `!*/` See https://stackoverflow.com/a/11853075/7976758 and https://stackoverflow.com/a/19672434/7976758 Found in https://stackoverflow.com/search?q=%5Bgitignore%5D+recursive+include+directories – phd Aug 14 '22 at 12:13

1 Answers1

1

The problem with A/* is that it matches A/B and when a directory first is excluded, children of it are unavailable ("It is not possible to re-include a file if a parent directory of that file is excluded").

So when excluding everything under A there is no way out of including all the directories again. You can however split the directories and pyhton files re-inclusion like the following:

A/*

!A/B
A/B/*
!A/C
A/C/*

!**/*.py
hlovdal
  • 26,565
  • 10
  • 94
  • 165