git 排除文件夹中除子目录之外的所有内容

jro*_*004 1 git node.js

我正在开发一个 Node js 项目,并且正在创建一个 Node 模块,目前希望将其添加到我正在开发的项目中。所以在我的 gitignore 中我输入了以下内容,但它不起作用

node_modules/*
!node_modules/zephyr-rest
Run Code Online (Sandbox Code Playgroud)

zephyr-rest 是一个目录,所以我只想保存该目录及其文件。我不希望对 node_modules 下的任何其他子目录进行版本控制。谢谢

Mri*_*jay 5

试试这个,对我有用。

node_modules/*
!node_modules/zephyr-rest/
Run Code Online (Sandbox Code Playgroud)

之后git status就会显示这样

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  node_modules/
Run Code Online (Sandbox Code Playgroud)

不用担心,因为git status只是告诉最顶层的未跟踪文件夹不是untracked每个子文件夹,所以这表明存在一个未跟踪的文件夹node_modules。做git add node_modules应该添加只是node_modules/zephyr-rest/