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.
Here's how I did it:
This is what worked for me. Allows you to ignore everything except specific files or folders
macOS sierra
modify the first line
change it to
I needed to ignore everything but not one folder with subdirectories.
For me, this works
For those looking for a cleaner solution, please try the following.
As mentioned in the comments of this answer, you have to use this method recursively.
In this example, you have a website setup at
./
where your.git
folder and.gitignore
file is located and a WordPress installation setup in./wordpress
. To correctly ignore the everything under the./wordpress
directory apart from the theme directory itself (wordpress/wp-content/themes/my-theme
), you will have to recursively ignore and allow each directory up until the directory you wish to allow:The reason for ignoring with a wildcard and allowing (or, ignoring 'apart from') the directory itself enables Git to look inside the directory first before ignoring everything inside. We then tell Git to ignore everything 'apart from' the directory we have specified. Here's the same syntax but in order of how Git is looking at it:
Hopefully this helps someone better understand the need for the recursive method.
If you prefix a pattern with an exclamation point (
!
) it negates any previous pattern which excluded it. So, presumably, you could ignore everything, then only allow what you want by using this pattern.I always get stuck somewhere on this even after coming back to this question numerous times. I've come up with a detailed process of doing it step by step:
First just use
git add
to add the actual content.It'll show the relevant files added to the index while all others still untracked. This helps contructing
.gitignore
step by step.Add a temporary
DUMMY.TXT
file in your directory:Our goal now is to construct the rules such that this
DUMMY.TXT
be the only one still showing up as Untracked when we're done.Start adding the rules:
.gitignore
First one is just to ignore everything. Untracked files should be all gone, only indexed files should be showing:
Add the first dir in the path
wp-content
Now the Untracked files will show up again, but only have
wp-content
's contentsIgnore everything in the first dir
/wp-content/*
and un-ignore!/wp-content/themes
Now the Untracked files will further narrow down to only
wp-content/themes
Repeat the process till that dummy file is the only one still showing as Untracked: