【问题标题】:How to use .gitignore如何使用.gitignore
【发布时间】:2018-09-22 13:25:11
【问题描述】:

我想排除 node_modules 目录,除了一个文件。

这就是我的.gitignore 的相关部分的样子 - 我尝试了一些东西但没有运气:

.gitignore

....

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

....

我想我需要做一些类似的事情:

node_modules/
!node_modules/path/to/index.js

但它说here 由于某些git 限制,上述内容将不起作用。无论如何,答案可能在那个链接中,我只是有点想不通。

【问题讨论】:

标签: node.js git github path gitignore


【解决方案1】:

你的方式:

node_modules/
!node_modules/path/to/index.js

不!由于 Git 中与性能相关的怪癖,您不能否定由于模式匹配目录而被忽略的文件。

我不知道怎么做

git add . 

添加your index.js 并排除所有文件node_modules

但是你可以使用git add node_modules/path/to/index.js -f

-f 因为您的文件被您的 .gitignore 文件之一忽略

并在.gitignore 中使用node_modules/

git 不会忽略 your index.js 文件(将其添加到缓存中)。

【讨论】:

  • 是的,如果您在git add your_file 之前将node_modules 添加到.gitignore
【解决方案2】:

另一种解决方案是将 react-native-calendars 目录移到 node_modules 目录之外,例如将其移至目录名称 nmod2。然后在导入时添加var react-cal = require('./nmod2/react-native-calendars'); 之类的路径,而不是var react-cal = require('react-native-calendars');。这样您就可以将整个 node_modules 目录放在 .gitignore 中。希望对你有帮助

【讨论】:

  • 对不起,我不明白该怎么做,为什么 .log 文件是相关的?
  • 糟糕,我没有仔细阅读您的帖子。我以为你想排除这两个文件。我会更新我的答案
  • 哦,我明白了,您认为我正在尝试删除 .log 文件 - 这正是我的默认 .gitignore 文件。我正在尝试从node_modules 文件夹中删除一个文件。
  • 所以我需要提交我的整个node_modules 文件夹,然后进入 github 并删除除一个目录路径之外的所有文件夹?这看起来很奇怪,可能需要一段时间
  • 我需要排除我的node_modules 文件夹中的所有内容,除了一个文件...应该有一种方法可以使用链接中的模式来做到这一点
猜你喜欢
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 2022-07-09
  • 2018-06-22
  • 2013-03-30
相关资源
最近更新 更多