How to use .gitignore to include files in nested subdirectories


Typically .gitignore is used to ignore files – but what do you do when you want to include files?

The trick when including files is to make sure that you include the whole tree that you are traversing. Every single subdirectory must be included before the file you want to include. Also make sure you start with * on an empty line and that everything you want to include (also the directories) start with an !

How to Use .gitignore to include files

In the example below we want to include accounts_import.csv, transactions_import.csv, and statement-1234.csv

Note who two sections are defined, the whole tree down to seeds and the whole tree down to statements

Normal git ignores


To check if it is working properly, use the command below:

git check-ignore -v -- /path/to/a/file/to/add



Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top