gitignore 不忽略配置文件

gitignore not ignoring config files

我已经读过 this question as well as this one 但它们似乎对我不起作用。

我有一个 git 项目,其目录结构如下:

myapp/
    src/
    .gitignore
    build/

其中 myapp/.gitignore 是:

### gitignore ###
build/

我现在需要添加一个顶级 config 目录,并希望将配置文件添加到其中以用于本地 development/testing 目的,但不希望它们被提交。我只想提交 ("empty") config 目录。所以我阅读了上面的这两个问题并尝试实施他们的建议。现在我的项目看起来像:

myapp/
    src/
    .gitignore
    build/
    config/
        .gitignore
        test.json

其中 myapp/.gitignore 是:

### gitignore ###
build/
!config/.gitignore

config/.gitignore 只是:

!.gitignore

但是,当我执行 git add . -n(干 运行)时,我看到:

add '.gitignore'
add 'config/.gitignore'
add 'config/test.json'

...所以看起来它无论如何都会尝试添加我的 test.json 配置文件。我只是想让它添加配置目录,以便命令输出:

add '.gitignore'
add 'config/.gitignore'

有什么地方会出错(以及为什么)吗?

config/.gitignore 应包含:

*
!.gitignore

也就是"ignore everything but .gitignore."这个:

!.gitignore

只是说 "don't ignore .gitignore",但并没有说 应该 被忽略。

(根 .gitignore 文件中也不需要 !config/.gitignore。)