1

I want ignore everything EXCEPT for one folder in my node_modules. So here is what I have put in my .gitignore:

!/node_modules/abc/*
/node_modules

Also tried:

!node_modules/abc/*
node_modules/*

But it is not working, as it is supposed to. This is making every folder inside node_modules getting ignored.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86

2 Answers2

2

The order of evaluation needs to be:

Ignore everything in node modules (note the trailing wildcard - this is important because otherwise, it just ignores the whole node_modules folder, and will not evaluate your nested folder, which you will un-ignore in the next line):

node_modules/*

EXCEPT:

!node_modules/abc/*

node_modules/*
!node_modules/abc/*

The * is important as otherwise you're ignoring the directory itself (so git won't look inside) instead of the files within the directory (which allows for the exclusion).

How do .gitignore exclusion rules actually work?

.gitignore exclude folder but include specific subfolder

limco
  • 1,310
  • 1
  • 15
  • 24
0

You'll have to do so in a .gitignore inside the node_modules directory, because it is not possible to re-include a file if a parent directory of that file is excluded. See the docs https://git-scm.com/docs/gitignore

Emanuel P
  • 1,586
  • 1
  • 6
  • 15
  • The OP ignored `node_modules` so `.gitignore` inside the `node_modules` is not considered at all. – phd Feb 19 '23 at 10:55
  • @phd Right. So instead of that, he needs to ignore everything from inside `node_modules` except that one thing he wants. – Emanuel P Feb 19 '23 at 13:28