gitignore 包括从被忽略/排除的目录中特定子目录。

huangapple go评论63阅读模式
英文:

gitignore include specific sub-directory from ignored/excluded directory

问题

我想忽略除了 `node_modules` 中的一个文件夹之外的所有内容。这是我在 `.gitignore` 文件中设置的内容:

!/node_modules/abc/*
/node_modules


还尝试过以下设置:

!node_modules/abc/*
node_modules/*


但它没有按照预期工作,这导致 `node_modules` 中的每个文件夹都被忽略。
英文:

I want ignore everything EXCEPT for one folder in my node_modules. So here is what I have put in my .gitignore:

!/node_modules/abc/*
/node_modules

Also tried:

!node_modules/abc/*
node_modules/*

But it is not working, as it is supposed to. This is making every folder inside node_modules getting ignored.

答案1

得分: 2

执行顺序应为:

忽略 node_modules 中的所有内容(请注意末尾的通配符 - 这很重要,否则它将忽略整个 node_modules 文件夹,并不会评估您在下一行中取消忽略的嵌套文件夹):

node_modules/*

除非

!node_modules/abc/*

node_modules/*
!node_modules/abc/*
  • 很重要,否则您将忽略目录本身(因此 git 不会查看其中的内容),而不是目录中的文件(允许排除)。
英文:

The order of evaluation needs to be:

Ignore everything in node modules (note the trailing wildcard - this is important because otherwise, it just ignores the whole node_modules folder, and will not evaluate your nested folder, which you will un-ignore in the next line):

node_modules/*

EXCEPT:

!node_modules/abc/*

node_modules/*
!node_modules/abc/*

> The * is important as otherwise you're ignoring the directory itself (so git won't look inside) instead of the files within the directory (which allows for the exclusion).

https://stackoverflow.com/questions/3001888/how-do-gitignore-exclusion-rules-actually-work

https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder

答案2

得分: 0

你需要在 node_modules 目录内创建一个 .gitignore 文件,因为如果排除了某个文件的父目录,就无法重新包含该文件。请查看文档 https://git-scm.com/docs/gitignore

英文:

You'll have to do so in a .gitignore inside the node_modules directory, because it is not possible to re-include a file if a parent directory of that file is excluded. See the docs https://git-scm.com/docs/gitignore

huangapple
  • 本文由 发表于 2023年2月19日 09:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定