What's the difference between Git ignoring directory and directory/*?
up vote
107
down vote
favorite
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.
git gitignore notation dotfiles
add a comment |
up vote
107
down vote
favorite
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.
git gitignore notation dotfiles
3
Why not integrate your comment into your question? It would make the latter even more interesting:)
– jubobs
Sep 8 '14 at 7:47
1
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
A simple difference betweenbin
andbin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference withbin/*
– Colonel Panic
Sep 13 '14 at 13:28
add a comment |
up vote
107
down vote
favorite
up vote
107
down vote
favorite
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.
git gitignore notation dotfiles
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.
git gitignore notation dotfiles
git gitignore notation dotfiles
edited Sep 25 '14 at 18:45
igaurav
2,02912035
2,02912035
asked Sep 8 '14 at 0:03
Aaron Shen
3,18562962
3,18562962
3
Why not integrate your comment into your question? It would make the latter even more interesting:)
– jubobs
Sep 8 '14 at 7:47
1
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
A simple difference betweenbin
andbin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference withbin/*
– Colonel Panic
Sep 13 '14 at 13:28
add a comment |
3
Why not integrate your comment into your question? It would make the latter even more interesting:)
– jubobs
Sep 8 '14 at 7:47
1
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
A simple difference betweenbin
andbin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference withbin/*
– Colonel Panic
Sep 13 '14 at 13:28
3
3
Why not integrate your comment into your question? It would make the latter even more interesting
:)
– jubobs
Sep 8 '14 at 7:47
Why not integrate your comment into your question? It would make the latter even more interesting
:)
– jubobs
Sep 8 '14 at 7:47
1
1
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
A simple difference between
bin
and bin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
– Colonel Panic
Sep 13 '14 at 13:28
A simple difference between
bin
and bin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference with bin/*
– Colonel Panic
Sep 13 '14 at 13:28
add a comment |
4 Answers
4
active
oldest
votes
up vote
196
down vote
accepted
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.
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think thatwww/1.txt
and thenwww/
would do the same as the second approach...
– Naveed Butt
Sep 10 '14 at 4:58
add a comment |
up vote
8
down vote
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
I did test the above, and if you replace !/foo
with !/foo/*
, you do indeed get a different result.
Note
foo
Will exclude any file foo
, but
foo/
will only exclude directories named foo.
add a comment |
up vote
3
down vote
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 the www
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?
/
|- .gitignore (a)
- www
|- .gitignore (b)
|- 1.jpg
- 2.jpg
In the root .gitignore
(a), you don't say anything about the www
folder or its contents.
In the www/.gitignore
(b) you put the following:
# ignore all files in this folder except this .gitignore
*
!.gitignore
This way everything looks more organized (to me at least).
add a comment |
up vote
1
down vote
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore
:
www/[^.]*
So no need for an extra .gitignore
, just simply add a .keep
file to your www
directory.
add a comment |
protected by Ionică Bizău Sep 14 '14 at 14:34
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
196
down vote
accepted
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.
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think thatwww/1.txt
and thenwww/
would do the same as the second approach...
– Naveed Butt
Sep 10 '14 at 4:58
add a comment |
up vote
196
down vote
accepted
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.
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think thatwww/1.txt
and thenwww/
would do the same as the second approach...
– Naveed Butt
Sep 10 '14 at 4:58
add a comment |
up vote
196
down vote
accepted
up vote
196
down vote
accepted
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.
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.
edited Sep 8 '14 at 2:58
answered Sep 8 '14 at 2:45
Landys
4,40921529
4,40921529
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think thatwww/1.txt
and thenwww/
would do the same as the second approach...
– Naveed Butt
Sep 10 '14 at 4:58
add a comment |
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think thatwww/1.txt
and thenwww/
would do the same as the second approach...
– Naveed Butt
Sep 10 '14 at 4:58
2
2
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
+1 for the practical example at the end illustrating how they're different in the negation pattern case. Enjoy the badge :)
– Benjamin Gruenbaum
Sep 9 '14 at 12:12
Don't you think that
www/1.txt
and then www/
would do the same as the second approach...– Naveed Butt
Sep 10 '14 at 4:58
Don't you think that
www/1.txt
and then www/
would do the same as the second approach...– Naveed Butt
Sep 10 '14 at 4:58
add a comment |
up vote
8
down vote
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
I did test the above, and if you replace !/foo
with !/foo/*
, you do indeed get a different result.
Note
foo
Will exclude any file foo
, but
foo/
will only exclude directories named foo.
add a comment |
up vote
8
down vote
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
I did test the above, and if you replace !/foo
with !/foo/*
, you do indeed get a different result.
Note
foo
Will exclude any file foo
, but
foo/
will only exclude directories named foo.
add a comment |
up vote
8
down vote
up vote
8
down vote
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
I did test the above, and if you replace !/foo
with !/foo/*
, you do indeed get a different result.
Note
foo
Will exclude any file foo
, but
foo/
will only exclude directories named foo.
I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, e.g.
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
I did test the above, and if you replace !/foo
with !/foo/*
, you do indeed get a different result.
Note
foo
Will exclude any file foo
, but
foo/
will only exclude directories named foo.
edited Sep 8 '14 at 1:19
answered Sep 8 '14 at 1:08
djechlin
41k20112223
41k20112223
add a comment |
add a comment |
up vote
3
down vote
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 the www
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?
/
|- .gitignore (a)
- www
|- .gitignore (b)
|- 1.jpg
- 2.jpg
In the root .gitignore
(a), you don't say anything about the www
folder or its contents.
In the www/.gitignore
(b) you put the following:
# ignore all files in this folder except this .gitignore
*
!.gitignore
This way everything looks more organized (to me at least).
add a comment |
up vote
3
down vote
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 the www
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?
/
|- .gitignore (a)
- www
|- .gitignore (b)
|- 1.jpg
- 2.jpg
In the root .gitignore
(a), you don't say anything about the www
folder or its contents.
In the www/.gitignore
(b) you put the following:
# ignore all files in this folder except this .gitignore
*
!.gitignore
This way everything looks more organized (to me at least).
add a comment |
up vote
3
down vote
up vote
3
down vote
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 the www
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?
/
|- .gitignore (a)
- www
|- .gitignore (b)
|- 1.jpg
- 2.jpg
In the root .gitignore
(a), you don't say anything about the www
folder or its contents.
In the www/.gitignore
(b) you put the following:
# ignore all files in this folder except this .gitignore
*
!.gitignore
This way everything looks more organized (to me at least).
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 the www
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?
/
|- .gitignore (a)
- www
|- .gitignore (b)
|- 1.jpg
- 2.jpg
In the root .gitignore
(a), you don't say anything about the www
folder or its contents.
In the www/.gitignore
(b) you put the following:
# ignore all files in this folder except this .gitignore
*
!.gitignore
This way everything looks more organized (to me at least).
answered Sep 9 '14 at 9:08
Carlos Campderrós
14k64253
14k64253
add a comment |
add a comment |
up vote
1
down vote
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore
:
www/[^.]*
So no need for an extra .gitignore
, just simply add a .keep
file to your www
directory.
add a comment |
up vote
1
down vote
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore
:
www/[^.]*
So no need for an extra .gitignore
, just simply add a .keep
file to your www
directory.
add a comment |
up vote
1
down vote
up vote
1
down vote
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore
:
www/[^.]*
So no need for an extra .gitignore
, just simply add a .keep
file to your www
directory.
To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore
:
www/[^.]*
So no need for an extra .gitignore
, just simply add a .keep
file to your www
directory.
edited Sep 9 '14 at 22:38
answered Sep 9 '14 at 22:25
Koen.
14.9k56069
14.9k56069
add a comment |
add a comment |
protected by Ionică Bizău Sep 14 '14 at 14:34
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
3
Why not integrate your comment into your question? It would make the latter even more interesting
:)
– jubobs
Sep 8 '14 at 7:47
1
Thanks Jubobs for your suggestion. I've integrated my comment into my question.
– Aaron Shen
Sep 8 '14 at 12:14
A simple difference between
bin
andbin/
is that the former will ignore files or folders, the latter only folders. I don't know the difference withbin/*
– Colonel Panic
Sep 13 '14 at 13:28