Amiga500
Amiga500

Reputation: 6141

Exact way of ignore/unignore files in git

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.

Upvotes: 3

Views: 3383

Answers (3)

Mario
Mario

Reputation: 36537

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.

Upvotes: 2

Kao
Kao

Reputation: 7374

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

Upvotes: 8

VonC
VonC

Reputation: 1327784

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.

Upvotes: 7

Related Questions