Is it possible to ignore all files in a subdirectory tree except a specific extension with .gitignore?
04:12 16 Apr 2026

I have a sub-directory hierarchy which largely includes files I don't want to track with git but with one extension I do want to include. In my case I want to track *.usmap but nothing else under a sources directory.

This blog suggests that it can be done with the following in the .gitignore:

# Ignore all files but include only .usmap files
sources/**
!sources/**/*.usmap

I also found this question and this where the answers suggest I should be able to do:

sources/**
!sources/**/
!*.usmap

But neither of these seem to work for me and I found this in the .gitignore docs:

It is not possible to re-include a file if a parent directory of that file is excluded.

Is there a way I'm missing to achieve the result?

This is my full directory hierarchy, the files I want to keep are in the first three directories, the files I want to ignore are in all the directories, I don't mind being a bit more explicit but the exact set of files and directories is subject to change.

source\siu
source\sl
source\sw
source\
source\icons
source\siu\bp
source\siu\enums
source\siu\levels
source\siu\mapimg
source\sl\bp
source\sl\enums
source\sl\extra
source\sl\levels
source\sl\mapimg
source\sw\bp
source\sw\enums
source\sw\levels
source\sw\mapimg
source\sw\parse
git gitignore