nadlie nadlie - 1 month ago 12
Git Question

Git exclude all files that match a name in all folders expect folders named Views

I want to exclude all file named Web.config from every folder in my directory
expect when the parent folder is named 'Views'

Ex directory:

-Root

-ApiProject
-Web.config (should exclude)
-SomeOther.file (should allow)
-SubFolder (should allow)
-one.file (should allow)
-two.file (should allow)
-MvcProject
-Web.config (should exclude)
-Views
-Web.config (should allow)


I have tried via the below in the .gitignore file, but doesnt seem to be working. The Web.config in the Views folder is not being added

[Ww]eb.config

!**/Views/[Ww]eb.config

Answer

It should be

**/[Ww]eb.config
!**/Views/[Ww]eb.config

**/[Ww]eb.config makes Git ignore files named Web.config and web.config in any folder, and !**/Views/[Ww]eb.config overrides previous pattern to not ignore these files if they are located within Views folder.

Also keep in mind that gitignore only works for currently untracked files:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

See How to make Git “forget” about a file that was tracked but is now in .gitignore?