Search code examples
gitgitignore

.gitignore How to exclude all direct files of a specific folder but include all subfolders of that folder?


I want to exclude all direct files of the folder reports but include all subfolders. At first I didn't knew that there were any subfolders in this folder and I pushed reports/*, to not push any automatically generated reports. But now all the template files in the subfolders are marked as deleted in the staging area, and I want to fix this:

Exclude:

  • reports/report1.pdf
  • reports/report2.txt
  • reports/report3PlainFile
  • ...

Include:

  • reports/A/a_1_template.rpt
  • reports/A/a_2_template.rpt
  • reports/B/a_2_template.rpt
  • ...

I have tried:

    reports/*
    !reports/*/

and

    reports/*
    !reports/*/*

but it doesn't seem to work.

Now everyting in the reports folder is excluded. But I want to only exclude all direct files and include all subfolders (and their contents).


Solution

  • .gitignore file content:

    /*
    !/*/
    

    First ignore everything then exclude folders recursively from the last match (everything).

    Update

    folder/*
    !folder/*/