适用于 Node.js 的 `.gitignore`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14962451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suitable `.gitignore` For Node.js
提问by fdj815
Tree structure
树状结构
That's how my Node.js project is organized:
这就是我的 Node.js 项目的组织方式:
/
| - node_modules [+ INCLUDE]
| | - my-mod1
| | | - node_modules [- IGNORE]
| | | | - external-mod1
| | | | - external-mod2
| | | - src
| | | | - node_modules [+ INCLUDE]
| | | | | - my-mod2
| | | | | - my-mod3
| | - my-mod4
My plan
我的计划
When publishing my project to GitHub:
将我的项目发布到 GitHub 时:
- I wantto include
my-mods. - I don't wantto include the
external-mods.
- 我想包括
my-mods。 - 我不想包括
external-mods。
That means:
这意味着:
- I wantto include the top-level
/node_modulesfolder. - I don't wantto include
node_modulesfolders which are direct childsof a module folder. - But I wantto include
node_moduelsfolders which are childs of asrcfolder.
- 我想包括顶级
/node_modules文件夹。 - 我不希望包括
node_modules它们的文件夹直接孩子的模块文件夹。 - 但我想包含
node_moduels文件夹的子src文件夹。
What I did
我做了什么
I added the following lines to /.gitignore:
我添加了以下几行/.gitignore:
#################
## npm
#################
npm-debug.log
node_modules/
!/node_modules/
!src/node_modules/
My question
我的问题
Which .gitignorerulesdo I need to include the right node_modulesfolders (as described above)?
我需要哪些.gitignore规则来包含正确的node_modules文件夹(如上所述)?
Thanks - if anything's unclear, please comment.
谢谢 - 如果有什么不清楚的,请发表评论。
采纳答案by functionoid
Since your structure is quite organized you could use this pattern to accomplish the task.
由于您的结构非常有条理,您可以使用此模式来完成任务。
/node_modules/*/node-modules/
The above pattern will ignore node_modules under module folders, my-mod1,my-mod4 and others.
上述模式将忽略模块文件夹下的 node_modules、my-mod1、my-mod4 等。
The above line will still allow the node_modules under src directory to be included when you push to github.
当您推送到 github 时,上面的行仍将允许包含 src 目录下的 node_modules。
回答by ben.snape
node_modules/**/node_modulesshould work for what you are trying to do.
node_modules/**/node_modules应该为您正在尝试做的事情工作。
Tip: GitHub providesstandard .gitignore's for various languages like Node.
回答by fdj815
Thanks for your answers.
感谢您的回答。
I rethought the condition and decided to declared the condition the other way - it works perfectly:
我重新考虑了条件并决定以另一种方式声明条件 - 它完美地工作:
node_modules/*/node_modules
I hope this is able to help anybody in the future.
我希望这能够在未来帮助任何人。
回答by StuR
node_modules/*
!node_modules/**/node_modules
maybe?
也许?

