How to .gitignore files recursively

GitGitignore

Git Problem Overview


I'm trying to avoid the following pattern in my .gitignore file.

MyPrject/WebApp/Scripts/special/*.js
MyPrject/WebApp/Scripts/special/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*/*/*.js

We tried:

MyPrject/WebApp/Scripts/special/**.js
MyPrject/WebApp/Scripts/special/**/*.js

This however didn't work. This is git on Windows. Is there a more concise way to do this without repeating things?

Git Solutions


Solution 1 - Git

As of git 1.8.2, this:

MyPrject/WebApp/Scripts/special/**/*.js

Should work according to this answer. It also works for me in Windows 7 using Sourcetree 1.6.12.0 and the version of git that it installs (1.8.4-preview20130916).

To gitignore every file and folder under a directory recursively:

MyPrject/WebApp/Scripts/special/**

Solution 2 - Git

Following gitignore manual page:

> [...] git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname.

So, this clearly stands that there is no way to specify a certain amount of directories between two strings, like between special and js.

Nevertheless, you can have a .gitignore file per directory, so maybe in your case the following content

*.js

at the following place

MyPrject/WebApp/Scripts/special/.gitignore

would be sufficient?

Solution 3 - Git

This works for me in on osx.

lib64/**/__pycache__/
lib/**/__pycache__/
*.py[cod]
.ipynb_checkpoints/
**/.ipynb_checkpoints/
.DS_Store
**/.DS_Store

Solution 4 - Git

Try executing git rm -r --cached MyPrject/WebApp/Scripts/special/ Your mentioned directory might have been cached by git and will show up on untracked files. After clearing the cache make sure you have it mentioned on .gitignore.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoradView Question on Stackoverflow
Solution 1 - GitSamView Answer on Stackoverflow
Solution 2 - GitGuillaume LemaîtreView Answer on Stackoverflow
Solution 3 - GitJonathanView Answer on Stackoverflow
Solution 4 - GitPradipta SarmaView Answer on Stackoverflow