Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .gitignore to ignore everything but specific directories

My issue is that I have a bunch of WordPress websites in my git repo, of which I want to selectively commit only the content of my themes folders, while ignoring the rest of the redundant files found in WordPress.

I've used .gitignore files to ignore file types before, but can it be used the other way around- that is to ignore everything BUT a certain folder path?

root (git repo)
- / wordpress
- - / (WordPress Site 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes
- - / (WordPress Site 3)/wp-content/themes

Thanks-

UPDATE:

Based on the answers I did the following, but it's not working. Any ideas?

# Ignore everything:
*
# Except for wordpress themes:
!*/wp-content/themes/*

I've also tried the following variations:

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

None of these read my themes folders.

like image 211
Yarin Avatar asked Oct 20 '22 09:10

Yarin


1 Answers

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/**
like image 172
Yarin Avatar answered Oct 22 '22 21:10

Yarin