Git recursive unignore file types

Viewed 33

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.

1 Answers

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
Related