Boris Zinchenko
Boris Zinchenko

Reputation: 2292

How to ignore all subfolders in a folder with .gitignore

I have a folder in Git with some files in it. Files are a part my Git repository. When running my project some work directories appear in this folder. Directories can have any name and any nesting level with multiple sub-directories. I want to ignore all possible sub-directories appearing in this folder but still keep all files (not directories) sitting in root of my folder.

I've tried patterns:

/
*/
/*
/*/

None of above gives desired result. How it can be accomplished?

Many answers explain how to ignore folders with specific names, for instance Git ignore sub folders. But there seem no explanation on how to ignore all sub-folders with wild cards.

Upvotes: 18

Views: 42841

Answers (2)

kan
kan

Reputation: 28991

It's quite simple. Create the .gitignore in the folder with:

*/*

I.e. ignore all files which are in any folder of the current folder.

Upvotes: 15

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

Try this:

!folder/
folder/*

The first line excludes the top level from being ignored, and the second line ignores all subfolders.

Upvotes: 18

Related Questions