Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore all subfolders in a folder with .gitignore

Tags:

git

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.

like image 563
Boris Zinchenko Avatar asked Dec 21 '16 13:12

Boris Zinchenko


People also ask

Does Gitignore work in subfolders?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

Can Gitignore ignore folders?

Inside . gitignore , you can tell Git to ignore only a single file or a single folder by mentioning the name or pattern of that specific file or folder. You can also tell Git to ignore multiple files or folders using the same method.

How do I Gitignore the contents of a folder?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .


1 Answers

Try this:

!folder/
folder/*

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

like image 90
Tim Biegeleisen Avatar answered Sep 28 '22 03:09

Tim Biegeleisen