Search code examples
gitgitignore

How to exclude all extensions (but not PHP and JS) from .gitignore sub-folder?


I want to exclude in .gitignore:

  • admin folder completely
  • content folder completely, but only allow .php and .js extensions in there.

I tried this, but it also allows in admin:

**/admin
**/content
!.php
!.js

Solution

  • Try ignoring folders (afolder/ with trailing slash), but whitelisting folder files (afolder/*):

    admin/
    content/*
    !content/subfolder/**/*.php
    !content/subfolder/**/*.js
    

    Adding a dedicated .gitignore in content/ folder is another option, but that way, you can centralize all ignore rules in one .gitignore file at the root folder of your main repository.

    As usual, to check what rule is applied:

    git check-ignore -v -- a/file