Pretty old question, but since I was struggling with this problem, even now I decided that I would share the actual solution to the problem.
The fact is, you cannot pass empty directories to Git, as if they were ignored, this rule does not apply to .gitignore .
From https://git-scm.com/docs/gitignore
The final "/ **" matches everything inside . For example, "abc / **" matches all files inside the "abc" directory, relative to a .gitignore location with infinite depth.
A slash followed by two consecutive asterisks, then a match of zero or more directories. For example, "a / ** / b" matches "a / b", "a / x / b", "a / x / y / b", etc.
** Matches everything inside - literally everything - files and directories.
This leads us to another question in gitignore docs:
Additional prefix "!" what denies the pattern; any corresponding file excluded by the previous template is included again. It is not possible to re-include the file if the parent directory of this file is excluded. Git does not list excluded directories for performance reasons, so any templates on the contained files have no effect, no matter where they are defined. Place a backslash ("\") before the first "!" for patterns that begin with the literal "!" for example, "! Important! .txt".
If the pattern ends with a slash, it is deleted for the purpose after the description, but it will only find a match for the directory. In other words, foo / will match the foo directory and the paths below it, but will not match the regular file or the foo symlink (this is consistent with the way the pathspec method works in general in Git).
Now suppose we have the following dir structure:
/path/to/git/repo |-- .gitignore |-- /cache |-- somefile |-- README |-- /dir |-- somefile2 |-- README
And we want to ignore all files inside the cache / except for README files.
If we specify gitignore as follows:
/cache/** !README
Git will ignore everything except /cache/README . Another README will not be displayed because its /cache/dir directory has been excluded with /cache/** , and !README is not even applicable to it. To solve the problem, we need to specify gitignore as follows:
# Ignore everything inside cache/ dir /cache # And except for README files !README