verystrongjoe
verystrongjoe

Reputation: 3911

Gitignore file doesn't work as I want

I exclude every files in node_modules except some files. So I wrote code in git ignore file like the followings.

node_modules/angular/angular.js
!node_modules/angularfire/dist/angularfire.js
!node_modules/todomvc-app-css/index.css
!node_modules/todomvc-common/base.css
!node_modules/todomvc-common/base.js    

node_modules/

but it doesn't work. how could I fix this problem?

Upvotes: 1

Views: 869

Answers (2)

Benedikt Schmidt
Benedikt Schmidt

Reputation: 2278

I think the problem here is that you exclude the complete node_modules folder AFTER you excluded some files, which might overwrite your previous inclusions/exclusion. That means in the beginning you say you want to include and exclude some files in the node_modules folder, but later on you just tell git "well, nevermind, just ignore the whole folder". So turning the order around might be a good step to start with.

But we're probably not finished. Also when you exclude a whole folder instead of files, git won't be able to find any files inside that if you want to exclude them. Let's just assume you ignored the folder node_modules, but want to exclude some files inside of it after that. It probably won't work because there is no folder to check for those files anymore. This folder is actually ignored.

So what you could try was to ignore all the files in the folder instead and then make some exclusions of those files.

# ignore all files in the folder
node_modules/**/*

# but not those files
!node_modules/angularfire/dist/angularfire.js
!node_modules/todomvc-app-css/index.css
!node_modules/todomvc-common/base.css
!node_modules/todomvc-common/base.js 

Hope this works.

Upvotes: 2

VonC
VonC

Reputation: 1324278

As I detailed in "How do I add files without dots in them (all extension-less files) to the gitignore file?", there is mainly one rule to remember with .gitignore:

It is not possible to re-include a file if a parent directory of that file is excluded. ()
(: unless certain conditions are met in git 2.7+)

That means, when you exclude everything ('node_modules/'), you have to white-list folders, before being able to white-list files.

# Ignore everything under node_modules
node_modules/*

# Exceptions the subfolders
!node_modules/**/

# Exceptions the files (since the parent folders are not ignored)
!node_modules/angularfire/dist/angularfire.js
!node_modules/todomvc-app-css/index.css
!node_modules/todomvc-common/base.css
!node_modules/todomvc-common/base.js 

Upvotes: 2

Related Questions