-1

I want to exclude node_modules directory except for one file.

This is what the relevant part of my .gitignore looks like - I have tried a few things but no luck:

.gitignore

....

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

....

I think I need to do something like:

node_modules/
!node_modules/path/to/index.js

But it says here that the above won't work due to some git restriction. Anyway, the answer is probably in that link, I'm just having a little trouble figuring it out.

ewizard
  • 2,801
  • 4
  • 52
  • 110
  • Possible duplicate of [Recursively ignore all files inside a specific directory except .json files](https://stackoverflow.com/questions/52416391/recursively-ignore-all-files-inside-a-specific-directory-except-json-files) – phd Sep 22 '18 at 16:17
  • https://stackoverflow.com/search?tab=newest&q=%5bgit%5d%20.gitignore%20node_modules – phd Sep 22 '18 at 16:17

2 Answers2

2

Your way:

node_modules/
!node_modules/path/to/index.js

Nope! Due to a performance-related quirk in Git, you can not negate a file that is ignored due to a pattern matching a directory.

I don't sure way to

git add . 

to add your index.js and all file exclude node_modules

But you can use git add node_modules/path/to/index.js -f

-f because your file are ignored by one of your .gitignore files

and use node_modules/ in .gitignore

git won't ignore your index.js file (added it to cache).

hong4rc
  • 3,999
  • 4
  • 21
  • 40
0

Another solution is to move your react-native-calendars directory outside your node_modules directory, e.g move it to a directory name nmod2. and then when you import it, add the path like var react-cal = require('./nmod2/react-native-calendars'); instead of var react-cal = require('react-native-calendars');. So that you can put the whole node_modules directory in .gitignore. Hope it helps

Rio Hilmy
  • 600
  • 4
  • 10
  • sorry, i dont understand what to do, why are the .log files relevant? – ewizard Sep 22 '18 at 13:44
  • Oops, my bad for not reading your post carefully. I thought you want to exclude those 2 files. I'll update my answer – Rio Hilmy Sep 22 '18 at 13:48
  • oh i see, you think I am trying to remove the `.log` files - that is just what my default .gitignore file is. i am trying to remove a file from `node_modules` folder from being committed. – ewizard Sep 22 '18 at 13:49
  • so i need to commit my whole `node_modules` folder and then go onto github and remove all of it except one directory path? that seems weird and might take a while – ewizard Sep 22 '18 at 13:50
  • i need everything in my `node_modules` folder to be excluded except for one file...there should be a way to do this with the patterns in the link – ewizard Sep 22 '18 at 13:52
  • i could probably just keep the whole package folder ex. `!node_modules/react-native-calendars/` but i need to ignore the rest – ewizard Sep 22 '18 at 13:57
  • okay I misunderstood your intention twice now, my apologies. But do you already have the whole node_modules and its contents in your github repository? – Rio Hilmy Sep 22 '18 at 14:02
  • no, i guess I could do that but it would take me a while to delete everything around the file i want to leave...and how would that even make it work? im guessing it would change some config somewhere by noticing that i do that? – ewizard Sep 22 '18 at 14:02
  • try moving your react-native-calendars directory outside your node_modules directory, e.g move it to a directory name nmod2. and then when you import it, add the path like `var react-cal = require('./nmod2/react-native-calendars');` instead of `var react-cal = require('react-native-calendars');`. So that you can put the whole node_modules directory in .gitignore. I just tried moving my express library outside node_modules and it works – Rio Hilmy Sep 22 '18 at 14:22