What’s the difference between Git ignoring directory and directory/*?

There’re differences among www, www/ and www/*.

Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www.

I’ll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.

For www/, git ignores the directory www itself, which means git won’t even look inside. But for www/*, git checks all files/folders inside www, and ignores all of them with the pattern *. It seems to lead to the same results since git won’t track an empty folder www if all its child files/folders are ignored. And indeed the results will be no difference for OP’s case with www/ or www/* standalone. But it does make differences if it’s combined with other rules.

For example, what if we want to only include www/1.txt but ignore all others inside www?

The following .gitignore won’t work.

www/
!www/1.txt

While the following .gitignore works, why?

www/*
!www/1.txt

For the former, git just ignores the directory www, and won’t even look inside to include www/1.txt again. The first rule excludes the parent directory www but not www/1.txt, and as a result www/1.txt cannot be “included again“.

But for the latter, git first ignores all files/folers under www, and then includes one of them again which is www/1.txt.

For this example, the follwing lines in the documentation may help:

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.

Leave a Comment