3

In my project I have a gitignore file, that nicely has statement to ignore node_modules, such as:

########################
# node.js / npm
########################
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz


logs
results

node_modules

It works just as expected. Git does not see any change within node_modules.

I have changes in a file within node_modules that I would like to include in further commits, as it will change definitely. But at the same time I want to keep the rest of the node_modules ignored. This is example of what I need to "unignore":

 node_module/passport-saml/saml.js

Some time ago, I had the same issue. I have followed some instructions how to do it, but I ended up creating a mess... I remember I used git uncheck / untrack or something similar. It took me more time to fix the thing I broke while trying to "unignore" the file. At the end, I ended up manually changing the line of code on the git.

This time I really would like to do it properly.

Amiga500
  • 5,874
  • 10
  • 64
  • 117

3 Answers3

8

You can use ! before path in your .gitignore file to invert the pattern:

 !node_module/passport-saml/saml.js

From man page:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again

Kao
  • 7,225
  • 9
  • 41
  • 65
  • @Wexoni Sure. Check [this](http://stackoverflow.com/questions/2820255/how-do-negated-patterns-work-in-gitignore/2820310#2820310) out – Kao Oct 03 '14 at 07:35
  • I totally see me using this :). Thanks for this tip. – Amiga500 Oct 03 '14 at 07:45
  • This is the correct solution, [see this SO answer](https://stackoverflow.com/a/42723384/2223505) – Merlin Nov 26 '17 at 07:28
6

You can add that file, and start tracking it with the --force option:

git add --force node_module/passport-saml/saml.js

From git add man page:

-f
--force

Allow adding otherwise ignored files.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

You won't have to add any special exception, git already handles that, once the file has been added once.

To add a file that fits a filter in your .gitignore, just force it by adding the -f param:

git add -f node_module/passport-saml/saml.js

Once the file is added, it will be tracked like any other file, even though the ignore filter matches.

Just change it and then add it as usual:

git add node_module/passport-saml/saml.js

That's it. No need for any special rules or exceptions.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • `git add --force` instead of an unignore pattern will cause problems if the file or directory moves around. See [SO answer](https://stackoverflow.com/a/42723384/2223505) – Merlin Nov 26 '17 at 07:27