zingi
zingi

Reputation: 1367

.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:

Include:

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).

Upvotes: 3

Views: 610

Answers (3)

Xaqron
Xaqron

Reputation: 30837

.gitignore file content:

/*
!/*/

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

Update

folder/*
!folder/*/

Upvotes: 1

zingi
zingi

Reputation: 1367

Actually:

    reports/*
    !reports/*/

worked. The problem was somewhere else.

I pushed reports/* on Branch A and merged it into Branch B.

So all the template files were still present on Branch A but were marked as deleted in the staging area of Branch B. So I switched back to Branch A and added !reports/*/ in the .gitignore file. Then I merged Branch A a second time into Branch B and discarded everything from the staging area of Branch B and now the subfolders (and content) are back in Branch B.

Upvotes: 0

Antonis
Antonis

Reputation: 11

*Please note that in .gitignore you only exclude files. *To be focused on your question the answer is that you have to navigate to each file manually and let the nested paths intact.

But in general:

First of all you have to start your .gitignore file with

*/**/.gitignore

,so that the git not take this file to be shared and continue with

**/target/*   
**/*.xml

,the idea is to include eather the file itself as

filename.xml

,or by navigation with * as in the xml line you want to exclude all the files that are of type .xml while in the target you want to exclude all the files that this file contains.

Hope that helps you.

Upvotes: 0

Related Questions