Reputation: 1
I have the following files structure in my server:
app/webroot/media/avatars/original/1424293016.png
app/webroot/media/avatars/original/usrimg.jpg
app/webroot/media/avatars/thumb150/1424293016.png
app/webroot/media/avatars/thumb150/usrimg.jpg
app/webroot/media/avatars/thumb350/1424293016.png
app/webroot/media/avatars/thumb350/usrimg.jpg
app/webroot/media/avatars/thumb70/1424293016.png
app/webroot/media/avatars/thumb70/usrimg.jpg
I want to exclude all files inside the 'media' directory except 'media/avatars/thumb70' and all it's files.
I can achieve that up to the "avatars" directory but when I go deeper it just exludes the whole "media" directory.
My logic tells me that I should use this .gitignore:
# .gitignore file
app/webroot/media/*
!app/webroot/media/avatars/thumb70/*
Thanks in advance for any help.
Upvotes: 0
Views: 75
Reputation: 7737
You can do it as follows.
app/webroot/media/**
!app/webroot/media/**/
!app/webroot/media/avatars/thumb70/**
Just remember to include back all directories under the folder media
first with !app/webroot/media/**/
. It won't affect the ignoring results alone, since git doesn't track folders, but it can make the !
rules for the sub-directories be applied.
Upvotes: 1
Reputation: 60303
Ignoring a directory summarily ignores all its content. If you're going to punch through a wildcarded ignore, the **
wildcards make for cleaner specs:
app/webroot/media/**
!app/webroot/media/avatars/
!app/webroot/media/avatars/thumb70/
!app/webroot/media/avatars/thumb70/**
is the way to do it if your project might scale into hundreds of folders, but if you don't like the summary-exclusion feature just shut it off by appending !*/
:
app/webroot/**
!app/webroot/media/avatars/thumb70/**
# whole-directory excludes disabled in the above:
!*/
*
only matches one level so if you're not entirely ignoring a directory you have to use **
to wildcard all its contents.
It'd be nice if git would take unignoring a path to imply unignores for all the directories along that path, but nobody's taught it to do that yet and unintended consequences are always a worry.
If you want to entirely ignore only specific directories, list those after the !*/
line.
# don't do whole-directory ignores in general but make a special exception for any bin/
!*/
bin/
Upvotes: 1
Reputation: 32893
The man page states (emphasis mine):
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
So an ugly solution is:
# .gitignore file
app/webroot/media/*
!app/webroot/media/avatars
app/webroot/media/avatars/*
!app/webroot/media/avatars/thumb70/
Upvotes: 0