Search code examples
gitgitignore

How to ignore all directories in a folder that don't begin with @?


In my project, I have a src/apps/ directory. I want git to ignore all subdirectories that don't begin with an @ symbol (keep src/apps/@dir/ or src/apps/file).

package.json
    |
.gitignore
    |
   src/ - - -
            |
          apps/ - - -
                    |
              keep-this-file
                    |
              keep-this-file-too
                    |
              @keep-this-dir/
                    |
              @keep-this-dir-too/
                    |
              ignore-this-dir/
                    |
              ignore-this-dir-too/

Solution

  • The .gitignore file can also define exclusions for ignored files:

    src/apps/*/
    !src/apps/@*/
    

    This ignores all files in the src/apps directory except everything in that directory that starts with @.