Search code examples
gitgitignore

gitignore not ignoring config files


I have already read this question as well as this one but they do not seem to be working for me.

I have a git project with the following directory structure:

myapp/
    src/
    .gitignore
    build/

Where the myapp/.gitignore is:

### gitignore ###
build/

I now need to add a top-level config directory, and would like to add config files to it for local development/testing purposes, but do not want them committed. I only want the ("empty") config directory committed. So I read those two questions above and tried implementing their advice. Now my project looks like:

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

Where myapp/.gitignore is:

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

And where config/.gitignore is just:

!.gitignore

However when I do a git add . -n (dry run) I see:

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

...so it looks like its going to try and add my test.json config file anyways. I just want it to add the config directory, so that the command would output:

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

Any ideas where I'm going awry (and why)?


Solution

  • config/.gitignore should contain:

    *
    !.gitignore
    

    That is, "ignore everything but .gitignore." This:

    !.gitignore
    

    Just says "don't ignore .gitignore", but it doesn't say that anything else should be ignored.

    (You also don't need !config/.gitignore in the root .gitignore file.)