John Taylor
John Taylor

Reputation: 737

.gitignore and using partial file or folder matching

I am using my .gitignore and various wildcards to only push to a repo (shared with others) particular folders or subfolders. Some of those .gitignore statements involve doing a general ignore and excepting from that ignore certain other folders. For example.

Solved/ #this prevents any folders named Solved from being pushed Activities/ #this prevents any folders named Activities from being pushed !01-Excel/Activities #this allows Activities folders under this directory to be pushed despite the line above. !01-*/**/Activities #this permits any folders which begin with "01-" and have a subfolder that has a folder named Activities to be pushed, despite the line above.

Here is my question.

How to use wildcards in .gitignore to permit any folder which has a particular string somewhere it is name to be pushed.

I have a folder like this:

01-Excel/Activities/01-Ins_codeStart/Solved/
02-Excel/Activities/02-Stu_codeProgress/Solved/

I would like the git ignore to work such that the Solved folders under any directory with the string "Ins" anywhere in it are pushed, while the Solved folders under any other directory are not.

Here is what I have tried:

!01-Excel/Activities/*-Ins*/Solved
!01-Excel/Activities//**-Ins*/Solved
!01-Excel/Activities//??-Ins*/Solved

None of those work to enable pushing of the Solved folder under directories with "Ins" somewhere in their name.

I recognize this is sort of way in the weeds, but the ability to achieve this would be a huge time saver in my circumstance. Thanks.

Upvotes: 6

Views: 3487

Answers (3)

VonC
VonC

Reputation: 1323903

None of those work to enable pushing of the Solved folder under directories with "Ins" somewhere in their name.

You can check why with git check-ignore:

git check-ignore -v -- /path/.../Ins/Solved/aFile

I would like the .gitignore to work such that the Solved folders under any directory with the string "Ins" anywhere in it are pushed, while the Solved folders under any other directory are not.

Any time you need to "allowlist" a folder, you need to exclude all parent folders from the .gitignore.

That is because:

It is not possible to re-include a file if a parent directory of that file is excluded.

So you cannot ignore Solved/ folder itself.
You can ignore or not Solved/ folder content (meaning its sub-files and subfolders)

# Exclude parent folders
!**/
# Ignore Solved folder content
Solved/**
# Exclude specific Solved folder content
!**/*-Ins*/Solved/**

Upvotes: 3

SwissCodeMen
SwissCodeMen

Reputation: 4875

If I have understand your question correct, you want to ignore the folder Solved if this folder Solved isn't a subfolder of a folder where the string "Ins" contains.

Then you would have to include the following in your .gitignore-file:

# ignore the folder Solved every where in this repository
**/Solved

# expect when the folder Solved is a subfolder of a folder where the string  "Ins" contains
!**/*-Ins*/Solved

Upvotes: 0

Sebastian Sipos
Sebastian Sipos

Reputation: 143

Globbing recursive could work, try using

01-Excel/Activities/**/*-Ins*/Solved

Upvotes: 0

Related Questions