Using .gitignore to ignore everything but specific directories

Here’s how I did it – you essentially have to walk up the paths, you can’t wildcard more than one level in any direction:

# Ignore everything:
*

# Except for the themes directories:

!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*

Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.

This is only how it worked for me. If someone cares to offer a more informed explanation by all means.

Also, these answers helpful:
how-do-negated-patterns-work-in-gitignore
how-do-gitignore-exclusion-rules-actually-work


NOTE: I tried using double-wildcard ‘globs’ but according to this that functionality is system dependent and it didn’t work on my mac:

Did NOT work:

!**/wp-content/themes/
!**/wp-content/themes/**

Leave a Comment