I'm confused about what's the correct way to ignore the contents of a directory in git.
Assume I have the following directory structure:
my_project
|--www
|--1.txt
|--2.txt
|--.gitignore
What's the difference between putting this:
www
And this?
www/*
The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:
www
!*.gitkeep
It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:
www/*
!*.gitkeep
Then it works! So I think it must has some differences between the two approaches.
Apart from the perfectly good answers you have already obtained, you should note that you can have
.gitignore
anywhere in your project, including subfolders.So if you want to ignore all files inside
www
, but whant thewww
folder to be versioned, instead of using an empty.gitkeep
,.dummy
or whatever name you choose, why not use a.gitignore
there, telling to ignore all files?In the root
.gitignore
(a), you don't say anything about thewww
folder or its contents.In the
www/.gitignore
(b) you put the following:This way everything looks more organized (to me at least).
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
I did test the above, and if you replace
!/foo
with!/foo/*
, you do indeed get a different result.Note
Will exclude any file
foo
, butwill only exclude directories named foo.
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your
.gitignore
:So no need for an extra
.gitignore
, just simply add a.keep
file to yourwww
directory.There're differences among
www
,www/
andwww/*
.Basically from the documentation and my own tests,
www
find a match with a file or a directory,www/
only matches a directory, whilewww/*
matches directories and files insidewww
.I'll only discuss on the differences between
www/
andwww/*
here, since the differences betweenwww
andwww/
are obvious.For
www/
, git ignores the directorywww
itself, which means git won't even look inside. But forwww/*
, git checks all files/folders insidewww
, and ignores all of them with the pattern*
. It seems to lead to the same results since git won't track an empty folderwww
if all its child files/folders are ignored. And indeed the results will be no difference for OP's case withwww/
orwww/*
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 insidewww
?The following
.gitignore
won't work.While the following
.gitignore
works, why?For the former, git just ignores the directory
www
, and won't even look inside to includewww/1.txt
again. The first rule excludes the parent directorywww
but notwww/1.txt
, and as a resultwww/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 iswww/1.txt
.For this example, the follwing lines in the documentation may help: